Forum Discussion

Matthias's avatar
Matthias
New Contributor III
3 years ago
Solved

Date.now() but with current timezone

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” ti...
  • RogerSramkoski's avatar
    3 years ago

    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.

    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.

    Try it yourself