SOLVED

Re: Set local timezone using Velocity scripts

Go to solution
Anonymous
Not applicable

Set local timezone using Velocity scripts

Hi all.

I am trying to use a script and Velocity to make the output of a date/time field more user friendly.

$date.format('MMMM dd, yyyy hh:mm a z', ${convert.parseDate(${lead.pexa_callbacktime}, 'yyyy-MM-dd')})

I am using the following Velocity script to achieve this, and so far I can successfully change the format, however it changes the timezone on me to GMT.  We are in Australia so need this to display AEST (or GMT+10).

The output i get is as follows.

Original Date/Time - 2016-09-14 11:00:00

Output after running through the above script - September 14, 2016 12:00 AM GMT

Any pointers on how to set the location or time zone in the output?  I am assuming this is picking up the timezone of Marketo's Apache server.

Thanks

Nick

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Set local timezone using Velocity scripts

When you first parse the Date from the string (inbound) include the timezone, and remember to include the time itself (you don't have it in your format pattern).

Instantiate the output timezone -- in the below snippet, in and out are the same -- and format the new string (outbound) with that timezone.

#set( $inTimeZone = $date.getTimeZone().getTimeZone('Australia/Sydney') )

#set( $outTimeZone = $date.getTimeZone().getTimeZone('Australia/Sydney') )

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

#set( $myDate = $convert.parseDate($lead.pexa_callbacktime,'MM-dd-yy HH:mm:ss',$locale,$inTimeZone) )

${date.format('MMMM dd, yyyy hh:mm a z',$myDate,$locale,$outTimeZone)}

View solution in original post

28 REPLIES 28
SanfordWhiteman
Level 10 - Community Moderator

Re: Set local timezone using Velocity scripts

When you first parse the Date from the string (inbound) include the timezone, and remember to include the time itself (you don't have it in your format pattern).

Instantiate the output timezone -- in the below snippet, in and out are the same -- and format the new string (outbound) with that timezone.

#set( $inTimeZone = $date.getTimeZone().getTimeZone('Australia/Sydney') )

#set( $outTimeZone = $date.getTimeZone().getTimeZone('Australia/Sydney') )

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

#set( $myDate = $convert.parseDate($lead.pexa_callbacktime,'MM-dd-yy HH:mm:ss',$locale,$inTimeZone) )

${date.format('MMMM dd, yyyy hh:mm a z',$myDate,$locale,$outTimeZone)}

Anonymous
Not applicable

Re: Set local timezone using Velocity scripts

Thanks Sanford, this is great!

Anonymous
Not applicable

Re: Set local timezone using Velocity scripts

Thanks Sanford, this has worked!

SanfordWhiteman
Level 10 - Community Moderator

Re: Set local timezone using Velocity scripts

Great. I'm going to do a short blog post on this as it seems to frequently be done incorrectly (people think their Velo tz and instance/Admin tz are the same).

Anonymous
Not applicable

Re: Set local timezone using Velocity scripts

Hi Sanford Whiteman

Im sorry to ask a silly question but how to I apply this to a token on a landing page

Thanks

SanfordWhiteman
Level 10 - Community Moderator

Re: Set local timezone using Velocity scripts

In an LP, where Velocity is not available, you can use a tz-aware library like Moment-Timezone.

Anonymous
Not applicable

Re: Set local timezone using Velocity scripts

Hi Sanford,

I'm looking to solve the same problem so our company can send a reminder email regarding a training. I'm not sure where I put this script. Is it in the email code header? Or do you add this to a snippet?

Also, I'm guessing I just need to adjust the '$lead.pexa_callbacktime' to the corresponding token field that I would have in this email 'company.Scheduled Training Date'.

#set( $inTimeZone = $date.getTimeZone().getTimeZone('Australia/Sydney') ) 

#set( $outTimeZone = $date.getTimeZone().getTimeZone('Australia/Sydney') ) 

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

#set( $myDate = $convert.parseDate($company.Scheduled Training Date,'MM-dd-yy HH:mm:ss',$locale,$inTimeZone) ) 

${date.format('MMMM dd, yyyy hh:mm a z',$myDate,$locale,$outTimeZone)} 

Cheers

Travis

SanfordWhiteman
Level 10 - Community Moderator

Re: Set local timezone using Velocity scripts

I'm looking to solve the same problem so our company can send a reminder email regarding a training. I'm not sure where I put this script. Is it in the email code header? Or do you add this to a snippet?

Neither: you create a new Email Script token and the code goes in the editor (left-hand side).

In the tree on the right-hand side, remember to check the boxes next to all fields you want to reference in the code -- in your case the Scheduled Training Date.  You should drag the field into the editor to find out the VTL (Velocity) name for the field.  It won't be the same as the normal token name because it can't have spaces. For example, {{lead.First Name}} as a normal token is $lead.FirstName in VTL.

Then the name of the Email Script token goes into the email using regular {{my.token}} syntax, for example {{my.velocity_training_date}} or whatever you call it.

Anonymous
Not applicable

Re: Set local timezone using Velocity scripts

I've set up the Email Script token as below. It re-arranges the date properly but still reverts to a different time zone.

Training date in salesforce:

24/05/2016 2:30PM

What shows up with the email token:

23 May, 2016 11:30 PM EST

#set( $inTimeZone = $date.getTimeZone().getTimeZone('Australia/Sydney') ) 

#set( $outTimeZone = $date.getTimeZone().getTimeZone('Australia/Sydney') ) 

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

#set( $myDate = $convert.parseDate($lead.Scheduled_Training_Date__c,'yy-MM-dd HH:mm',$locale,$inTimeZone) ) 

${date.format('dd MMMM, yyyy hh:mm a z',$myDate,$locale,$outTimeZone)} 

Any suggestions?