Forum Discussion

mtran21's avatar
mtran21
New Contributor III
5 years ago

Convert UTC date to date

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

11 Replies

  • dmiller's avatar
    dmiller
    Former Employee

    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.

  • dmiller's avatar
    dmiller
    Former Employee

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

    • mtran21's avatar
      mtran21
      New Contributor III

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

  • dmiller's avatar
    dmiller
    Former Employee

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

    • mtran21's avatar
      mtran21
      New Contributor III

      I got this error

      HIRE_DATE is a datetime data from Oracle

      • dmiller's avatar
        dmiller
        Former Employee

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

  • 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();
    
    • ForbinCSD's avatar
      ForbinCSD
      Contributor

      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/