Forum Discussion

shane's avatar
shane
New Contributor II
6 years ago

Can't create a non-UTC Date object to call .getTime() on

I need to pull data from an API that needs a timestamp as a parameter. This API retrieves data from a system (not in my control) that is based in the America/Las_Angeles timezone. I have tried for hours to find a way to generate a non-UTC Date object (LocalDateTime object doesn’t contain a getTime() method) so that I can call .getTime() but I seem to only be able to get a string value of the converted UTC Date into my desired timezone.

I tried to find a way to use the getTimezoneOffset() function, but I need a non-UTC Date object in order to get the valid offset from so I’m back to square one.

Can anyone provide an example of creating a Date object in Snap with a non-UTC timezone that I can call getTime()? (I just need to generate a timestamp based on a PST Date with a 00:00:00 time)

I tried creating the converted string (which I can’t get to include the timezone, because even though the docs point to SimpleDateFormat (Java Platform SE 7 ) as a reference I cannot get the value output for any of the time zone components) and appending ’ PST’ but it still created a UTC Date.

I have spent way too long trying to do something so simple that is a couple lines of code in almost any language and needs some guidance lol.

15 Replies

  • christwr's avatar
    christwr
    Contributor III

    Date.now().toLocaleDateString({"timeZone":"America/Los_Angeles", "format":"yyyy-MM-dd"}) + " 00:00:00"

    It will “handle” whether it’s currently PDT or PST.

    • shane's avatar
      shane
      New Contributor II

      That will get you a date-time string not a timestamp. How do you turn that into a timestamp?

  • tstack's avatar
    tstack
    Former Employee

    I’m not sure what you’re asking here since getTime(), by definition, returns the epoch timestamp, which is UTC.

    • shane's avatar
      shane
      New Contributor II

      I don’t think you read the issue fully. I DON’T want UTC based timestamp and you cannot call getTime() on LocalDateTime or LocalDate which is used to create a non-UTC Date object. I cannot find a way to get the timestamp for a non-UTC Date.

      It doesn’t matter I guess as I altered the called service to accommodate things.

      • tstack's avatar
        tstack
        Former Employee

        In general, a timestamp is the number of seconds/milliseconds from the epoch, where the epoch is defined as January 1st, 1970 00:00:00 UTC. So, a timestamp is pretty much always going to be based in UTC. Asking for a non-UTC timestamp is highly unusual.

  • Supratim's avatar
    Supratim
    Contributor III

    @shane

    You can use Date.now().toLocaleDateTimeString(’ { “timeZone”:“EST” , " format" :" YYYY-MM-DD" }')

    • shane's avatar
      shane
      New Contributor II

      That will give you a date-time string but not a timestamp. Try to create a timestamp from that and that is my issue.

      • stevegoodwin's avatar
        stevegoodwin
        Employee

        if you want to adjust a GMT timestamp to PST (-7), what about:
        Date.now().getTime() + (3600 * -7)
        or
        Date.parse(Date.now().plusHours(-7)).getTime()

  • christwr's avatar
    christwr
    Contributor III

    Date.parse(Date.now().toLocaleDateString({"timeZone":"America/Los_Angeles"})).getTime()

    • shane's avatar
      shane
      New Contributor II

      So that is the first thing I tried, but it gives me a UTC DateTime object at 00:00:00 for the Date, and calls getTime() on it. You cannot call getTime() without the Date.parse() which turns it into a UTC timezone because toLocalDateString() and toLocalDateTimeString() both return a datetime string WITHOUT the timezone or offset. Even though the snaplogic documentation says you can provide a ‘x’ or a ‘z’ in the ‘format’ parameter the result doesn’t contain any timestamp information for Date.parse() to understand. I even tried appending the timezone information to what is passed into the Date.parse() with no luck.

      This will result in a UTC Y-m-d 00:00:00 timestamp.

      I need a America/Los_Angeles Y-m-d 00:00:00 timestamp. If I used the above, I will be off by whatever the offset is in the America/Los_Angeles timezone.

  • Schevus's avatar
    Schevus
    New Contributor III

    I’m running into this problem as well. I need to convert a value obtained from toLocaleDateTimeString() back to a SnapLogic date object in its current timezone, but there doesn’t seem to be any way to maintain or set the timezone when parsing back to a date object. It always reverts to UTC as the timezone, though with the “correct” time from the original timezone. This is really unfortunate.

    Does anyone know how to create a new date object (not derived from a datasource with a non-UTC timezone) that is not in UTC time?

  • gsuresh92's avatar
    gsuresh92
    New Contributor

    Date.parse(Date.now().toLocaleDateString({“timeZone”:“America/Los_Angeles”, “format”: “yyyy-MM-dd’T’00:00:00Z”})).getTime() ?