javascripttimestamp

compare timestamps in javascript


I have a date saved in a string with this format: 2017-09-28T22:59:02.448804522Z this value is provided by a backend service.

Now, in javascript how can I compare if that timestamp is greater than the current timestamp? I mean, I need to know if that time happened or not yet taking in count the hours and minutes, not only the date.


Solution

  • That looks like a ISO8601 date string. You can parse it with Date's constructor and use the built-in comparison operators.

    new Date('2017-09-28T22:59:02.448804522Z') > new Date()
    // true
    new Date('2017-09-28T22:59:02.448804522Z') < new Date()
    // false