cancel
Showing results for 
Search instead for 
Did you mean: 

Date.now() but with current timezone

Matthias
New Contributor III

Is there a way to get the current time in the snapplex’s timezone? (or could this be filed as important platform enhancement?)

using date.now() returns the date in UTC, but i’d need it in “my” timezone (or in a random timezone, really).

The closest i could come up with is the following - which is obviously very cumbersome for a simple “now” function in a particular timezone.
Date.parse(Date.now().toLocaleDateTimeString({"timeZone":"Europe/Rome"}) + " Europe/Rome")

Unless i’ve missed in the documentation, it doesn’t appear like snaplogic supports any kind of Date.now().asTimeZone('Europe/Rome') functionality

1 ACCEPTED SOLUTION

RogerSramkoski
Employee
Employee

Hello @matthias.voppichler! I think what you have is accurate. If this is something you find yourself using a lot you could look at adding it to an Expression Library. In case you aren’t familiar with Expression Libraries, I have a working example for you (or anyone else) below with the files below for download.

Expression library contents:

First we need to create the expression library outside of SnapLogic in a text editor. We only need this line in the expression library to make it work. I saved it as ‘datehelper.expr’ but you can name the file whatever works for you as long as it is a *.expr file extension. You can also rename the function ‘DateWithTZ’ to whatever works for you.

{
    DateWithTZ: x => Date.now().toLocaleDateTimeString({"timeZone":x.toString()}) + " " + x.toString()
}

Add the Expression Library to Pipeline Properties

Basically this is the same concept as importing a library or module into our code. We just need to make sure the pipeline knows it can use this Expression Library and optionally give it an alias.
image

Using the Expression Library functions

Now in the Mapper Snap we can call our library and function with lib.datehelper.DateWithTZ() and for this specific example, we’ll pass “Europle/Rome” as the time zone. The first expression is the long one that would normally be needed to accomplish the goal and the second expression is using the expression library.
image

Try it yourself

View solution in original post

8 REPLIES 8

I wouldn’t have the “asTimezone” method output a string - but just convert the actual date to be in the correct timezone (the output would still be a date object).

that’s right, you will have to use a function .toString() for that.

darshthakkar
Valued Contributor

@Matthias - Try using this too

Date.now().toLocaleDateTimeString({"format":"yyyy-MM-dd'T'HH:mm:ss:SSS z","timeZone":"America/New_York"})

@Matthias -
Replace America/New_York with the intended timezone, available timezones can be found here: https://joda-time.sourceforge.net/timezones.html