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

6 REPLIES 6

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

That looks like a great solution
is it possible to add a generic expression library to the whole account, to make such a library available to all Pipelines at once, without re-uploading the same file over and over again?

Yes, you can have an expression library on “shared” under a project space so that all the projects underneath can use that.

darshthakkar
Valued Contributor

Hi @matthias.voppichler,

The string that you have been using is correct, as far as I’ve used Date/Time in my pipeline, I have used the same. My string looks like:

Date.now().toLocaleDateTimeString({"format":"yyyyMMddHHmm","timeZone":"America/New_York"})

I also use the settings in my snowflake (if you have one) snap as below:
image

I believe the format which snapLogic supports/provides is pretty straightforward, any tool/language would have its own syntax and the one that has been working for you is for snapLogic.

For someone like me whose use case is to define a date/time in a specific format, even the string you’re suggesting would grow, it would be something like:

Date.now().asTimeZone(‘Europe/Rome’,“format”:“yyyyMMddHHmm”) OR

Date.now().asTimeZone(“format”:“yyyyMMddHHmm”,‘Europe/Rome’)

There might be other use cases where you’ll have to consider days/weeks/months after current day and in a particular format, someone might have a use case to go ahead with a sequence on which the pipeline will run so on and so forth. Thus, the string will grow every now and then depending on the use case.

In my honest opinion, the one that we have is easy to use however if it is a lot of work, you can always use an expression library and define it however the use cases change and you will eventually end up defining multiple rows/functions of date/time in your expression library.

Thank you.

Regards,
Darsh