SOLVED

Get time from timestamp

Go to solution
Rasmus_Bidstru1
Level 4

Get time from timestamp

Hi

I got at timestamp on a lead, but I want to show the date one place, and the time another place in an email.

So this:

2016-09-06 11:00:00

Should be:

11:00

And

this:

2016-09-06 11:00:00

Should be:

06-09-2016

My guess is that it involve some $dateformat with email scripting, but not sure how its written.

1 ACCEPTED SOLUTION

Accepted Solutions
Rasmus_Bidstru1
Level 4

Re: Get time from timestamp

Now it works!

I just needed to remove the ss in 'yyyy-MM-dd HH:mm:ss'

View solution in original post

12 REPLIES 12
Grégoire_Miche2
Level 10

Re: Get time from timestamp

Hi Rasmus,

Yes, indeed, you will need Velocity scripting. You will have to create 2 tokens.

See here:

DateTool (VelocityTools 2.0-beta4 Documentation)

Manipulating a date-time with Velocity

Adding Days and Comparing Dates

-Greg

SanfordWhiteman
Level 10 - Community Moderator

Re: Get time from timestamp

Both of those blog articles are off. The first omits timezones, which is a critical consideration in a Marketo context. The second tries to manually set offsets based on short timezone strings, which will inevitably fail because of daylight savings.

See my answer here: Set local timezone using Velocity scripts

Rasmus_Bidstru1
Level 4

Re: Get time from timestamp

But this has nothing to do with timezone and local time, I just want the Time (HH:mm) from a timestamp a lead have on the profile.

Otherwise i could make it to a string, and just take the last 5 charaters which is the time.

SanfordWhiteman
Level 10 - Community Moderator

Re: Get time from timestamp

It always has to do with timezone and local time.  You can't parse a string into a Date object without having a timezone, either an explicit timezone passed to parseDate or the implicit timezone of the Velocity engine (which is taken from your pod, not your instance).

In order to not have time shifts, always pass the timezone to the parser. Those subtle shifts of an hour are not so subtle when scheduling appointments.

Rasmus_Bidstru1
Level 4

Re: Get time from timestamp

But its not a string to Date Object, its a fieldtype of Datetime. Does that change anything?

SanfordWhiteman
Level 10 - Community Moderator

Re: Get time from timestamp

Please read this: http://blog.teknkl.com/marketo-vtl-strings-all-the-way-down/

Strings have no timezones (nor even dates or times -- they're just strings).  You're doing a more dramatic transformation than you might think when you run ​parseDate.

Rasmus_Bidstru1
Level 4

Re: Get time from timestamp

Hmm it dosnt work. When I put a typed string in, it works, but when i use my lead. attribute, it dosnt work anymore.

#set($mytime = ${lead.dKSmartstarttidspunkt})

$mytime (this displays as 2016-09-05 10:20:10)

They i try to use it in the code, and it just shows nothing 😞

#set( $inTimeZone = $date.getTimeZone().getTimeZone('America/New_York') )

#set( $outTimeZone = $date.getTimeZone().getTimeZone('America/New_York') )

#set( $locale = $date.getLocale() )

#set( $myDate = $convert.parseDate(${lead.dKSmartstarttidspunkt},'yyyy-MM-dd HH:mm:ss',$locale,$inTimeZone) )

#set( $dateOnly = $date.format('dd-MM-yyyy',$myDate,$locale,$outTimeZone) )

#set( $timeOnly = $date.format('hh:mm',$myDate,$locale,$outTimeZone) )

$timeOnly

$dateOnly

Rasmus_Bidstru1
Level 4

Re: Get time from timestamp

Now it works!

I just needed to remove the ss in 'yyyy-MM-dd HH:mm:ss'

SanfordWhiteman
Level 10 - Community Moderator

Re: Get time from timestamp

The date-like string can be longer than the formatting pattern. The formatting pattern cannot be longer than the date-like string (it cannot refer to placeholders that don't exist in the string).

You originally said your dates were formatted like "2016-09-06 11:00:00" -- thus including seconds ('ss'). If the code doesn't work with your token, then your token isn't formatted the way you originally said. That doesn't mean the code isn't correct.