Forum Discussion

aleung's avatar
aleung
Contributor III
8 years ago
Solved

How to obtain difference btw 2 timestamp?

Any idea how to obtain seconds, minutes, or hours between 2 timestamp?

  • In the above example just dont use the quotes, instead send as JSON object!! Otherwise you will get the warning from the platform at runtime.

    Date.now().toLocaleDateString({“timeZone”:“PST”, “format”:“yyyy-MM-dd”})
    

13 Replies

    • aleung's avatar
      aleung
      Contributor III

      I already tried it. It works with Date.now().getTime() but the following example didn’t
      e.g. $extracts_refreshed_at.getTime() <== where $extracts_refreshed_at is _snaptype_localdatetime

      Here is the error I am getting:
      Failure: Unknown method: getTime, Reason: Object has an unknown type (org.joda.time.LocalDateTime), Resolution: Please check expression syntax

      • del's avatar
        del
        Contributor III

        @aleung, it’s not pretty, but I think you can solve with DateTime.parse($extracts_refreshed_at.toString()).getTime()

  • shane's avatar
    shane
    New Contributor II

    I’m not sure if something has changed, but his appears to not work anymore.

    It appears that the object returned from DateTime.parse() doesn’t contain a getTime() method.

    This leaves me with the same question that started this thread. Any idea how to obtain seconds, minutes, or hours between 2 timestamp?

    I am stuck doing something that seems ‘hacky’ (but it works):
    Date.parse('01/01/2019 ’ + $start_time).getTime()

    • tstack's avatar
      tstack
      Former Employee

      Are you sure $end_time is a parseable date string? If the parse fails, a NaN is returned which is a Double internally. Sorry that’s not more clear.

      You can move the cursor over to parse and $end_time to see what their values are. For example, the following shows the result of the parse is NaN with the same error message that you got:

      The Date.parse() method parses dates and dates with times, not just time.

      • shane's avatar
        shane
        New Contributor II

        I see that the screenshot I posted was showing the Date.parse() not the DateTime.parse(), however I assure you that is where that error came from.


        And here are the values for the fields used:

        Please feel free to try using DateTime.parse().getTime() and see if it works but it does not for me.

        Also a note to all doing time comparisons, you have to be weary of only having a timestamps but the dates may cross days. This works using the ‘hard-coded’ date string and Date.parse() but still seems a bit hacky for me:

        (Date.parse('01/02/2019 ' + $end_time).getTime() >= Date.parse('01/02/2019 ' + $start_time).getTime()) ? ((Date.parse('01/02/2019 ' + $end_time).getTime() - Date.parse('01/02/2019 ' + $start_time).getTime()) / 1000) : ((Date.parse('01/02/2019 ' + $end_time).getTime() - Date.parse('01/01/2019 ' + $start_time).getTime()) / 1000)