07-14-2023 12:36 AM
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
Solved! Go to Solution.
07-14-2023 07:07 AM
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.
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()
}
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.
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.
07-14-2023 07:37 AM
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).
07-14-2023 07:48 AM
that’s right, you will have to use a function .toString()
for that.
09-27-2024 02:27 PM
@Matthias - Try using this too
Date.now().toLocaleDateTimeString({"format":"yyyy-MM-dd'T'HH:mm:ss:SSS z","timeZone":"America/New_York"})
09-27-2024 02:32 PM
@Matthias -
Replace America/New_York with the intended timezone, available timezones can be found here: https://joda-time.sourceforge.net/timezones.html