cancel
Showing results for 
Search instead for 
Did you mean: 

Convert UTC date to date

mtran21
New Contributor III

How do you convert this date “2020-09-01T00:00:00.000” to this “20200901”?

11 REPLIES 11

ForbinCSD
Contributor

The built-in date handling in Javascript isn’t the best, and the engine we’re on in snapLogic isn’t the latest. We’ve been using moment.js successfully to good effect. It’s a bit large but on the server nobody cares about download speeds. If you have to do a lot of date and/or time handling, especially converting between time zones, you might want to check it out.

The two following statements get it loaded for your from the public server, but we don’t do it that way. Once we settled on using it, we downloaded it ourselves to our Project Spaces so it’s available locally on the snaplex.

load("https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.26.0/moment.min.js");
load("https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.31/moment-timezone-with-data.min.js");

Then we use moment’s parsing and time zone corrections.
For example:

var zones = moment.tz.names();
zones.unshift("Not Set");
var timeZonedDate = moment.tz(theDateTimeString, zones[timeZoneId]);
return timeZonedDate.utc();

The above assumes you’re getting a date and time in a particular time zone and want it in UTC. And the return value is a “moment” object, which is an extension of the JavaScript DATE object. It has several formatting methods you can use to get just the date portion as a string in “yyyymmdd” format if that’s what you need.

You might want to read the moment.js documentation and then play around with it. Note that the authors do not recommend using moment for new development. At my employer, we feel this advice does not apply here, because many of the newer libraries don’t work well in snapLogic, and moment does.

https://momentjs.com/timezone/

https://momentjs.com/docs/