0

I'm getting a time in a variable using AMP script, the problem is that the time is in another GMT config and I was not able to find a way to change that to a specif GMT time. For now I'm using this workaround:

set @date = NOW()
set @date = DateAdd(@date,2,"H")

Is there a way to get NOW() for my specific GMT?

2 Answers 2

1

Servertime is always UTC-6. Generally speaking you have SystemDateToLocalDate() to transform server to localtime.

%%[
SET @servertime = NOW()
SET @localtime = SystemDateToLocalDate(@servertime)
]%%

This probably is already solving most issues, as it will output local time from the execution context. It will respect for daylight savings time (DST).

You can also specifically get the offset between local and servertime with datediff between local and server:

%%[
/* timezone check */
SET @servertime = NOW()
SET @localtime = SystemDateToLocalDate(@servertime)
SET @timezoneOffsetinHours = DateDiff(@servertime,@localtime, 'H')

SET @localizedTimeStamp = DateAdd(@servertime,@timezoneOffsetinHours,"H")
]%%

Offset can be useful, e.g. to easily simulate different timezones, or reverse engineer the time difference for a specific point in time.

Example: Germany is 8 / 7 hours away from UTC-6 ("exacttarget time") in summer / winter:

%%[
/* timezone check with DST - was DST in effect in Germany at a specific time? */
 
SET @timedifference_summer = 8
SET @timedifference_winter = 7
SET @servertime = "" /* insert any date here */
SET @localtime = SystemDateToLocalDate(@servertime)
 
/* perform timezone check to apply german daylight saving time offset;*/
IF DateDiff(@servertime,@localtime, 'H') == @timedifference_summer THEN
    SET @timezoneOffset = @timedifference_summer 
ELSEIF DateDiff(@servertime,@localtime, 'H') == @timedifference_winter THEN
    SET @timezoneOffset = @timedifference_winter
ELSE
    SET @debugmessage = "not applicable"
ENDIF
SET @localizedTimeStamp = DateAdd(@servertime,@timezoneOffset,"H")

]%% Let's imagine we got a user registration on date %%=v(@servertime)=%% from Germany. The offset between german localtime and SFMC servertime was %%=v(@timezoneOffset)=%%. So for the user, the time was actually %%=v(@localizedTimeStamp)=%%.

As a general word of caution. This sort of acrobatics only makes sense when you communicate to external systems, display dates on webpages etc. The longer you can work with servertime, the easier all your processes become. see my rant here: Customize the default value in the date field

3
  • Thank you Jonas.
    – user87331
    Aug 14, 2020 at 13:48
  • Hi Jonas. This way seems depending on Time-zone user preferences due to using SystemDateToLocalDate. What about if I am at a different user time zone and want to convert the date time to a specific time-zone using DST in AMPscript?
    – Duc Le
    May 3, 2022 at 10:33
  • 1) please open a separate question so others can profit 2) "LocalDate" in systemDateToLocalDate does not respect user level as it would happen in Sales Cloud, it respects BU level timezone settings. May 3, 2022 at 11:39
0

And here's the code snippet I use when the email is triggered by a Salesforce data event. In that case, all the dates are in GMT+0 timezone, so you need to add offsets to that time, not to GMT-6.

%%[ 
SET @TimeDifference_summer = 8
SET @TimeDifference_winter = 7
SET @SchedStartTime = AttributeValue("ServiceAppointment:SchedStartTime") 
SET @ServerTime = NOW()
SET @LocalTime = SystemDateToLocalDate(NOW())

IF DateDiff(@ServerTime, @LocalTime, 'H') == @TimeDifference_summer THEN
  SET @TimezoneOffset = 2
ELSEIF DateDiff(@ServerTime, @LocalTime, 'H') == @TimeDifference_winter THEN
  SET @TimezoneOffset = 1
ELSE
  SET @TimezoneOffset = 0
ENDIF

SET @SchedStartDateWithOffset = DateAdd(@SchedStartTime, @TimezoneOffset, "H")
]%%

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .