Convert UTC date to date

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

I’m double checking, but I think you have to use one of the formats listed in the Parsing a Date example in the documentation.

@cjhoward18 to the rescue. :slight_smile:
Date.parse("2020-09-01T00:00:00.000").toLocaleDateString({"format":"yyyyMMdd"})

Thanks, will this work with variable?
Date.parse("$HIRE_DATE").toLocaleDateString({“format”:“yyyyMMdd”})

You shouldn’t need the quotes around the variable, but it should work if it’s a string.

I got this error

HIRE_DATE is a datetime data from Oracle

Then you should try:
$HIRE_DATE.toLocaleDateString({"format":"yyyyMMdd"})

Got this error

Try this:

Date.parse($HIRE_DATE.toString()).toLocaleDateString(...)

Absolutely beautiful. It worked
Date.parse($HIRE_DATE.toString()).toLocaleDateString({“format”:“yyyyMMdd”})

Thank you @ptaylor and @dmiller

1 Like

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.