Get time from timestamp | Community
Skip to main content
Level 4
September 5, 2016
Solved

Get time from timestamp

  • September 5, 2016
  • 2 replies
  • 7304 views

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.

Best answer by Rasmus_Bidstru1

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


Now it works!

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

2 replies

Grégoire_Miche2
Level 10
September 5, 2016

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
September 5, 2016

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

Level 4
September 5, 2016

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
September 5, 2016

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

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

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

#set( $myDate = $convert.parseDate("2016-09-06 11:00:00",'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) )

Level 4
September 5, 2016

Thanks :-)

And to print the time it should just be

#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

right? or is it ${dateOnly}
SanfordWhiteman
Level 10
September 5, 2016

You don't need ${formal} notation to output just the variable because there isn't any surrounding text, but you might as well use it.

I see a lot of people using ${formal} in #set statements where it isn't necessary, just adding characters and possible typos.