Javascript Compare UNIX Timestamps

I got two unix timestamps, and I need to find a way to compare them and find out which one is the newest (closer to the current date).

Two timestamps:

  • 1299925246
  • 1300526796

Is there an easy way to do this in Javascript?

+4
source share
2 answers

UNIX time is expressed as the number of seconds elapsed since January 1, 1970, 00:00:00 UTC.

The comparison is so simple: in your example, the second timestamp ( 1300526796 ) is the newest because 1300526796 (March 19, 2011, 09:26:36 UTC) is larger than 1299925246 (March 12, 2011, 10:20:46 UTC).

+9
source

Just ran into the same problem, but with a few timestamps, for example:

 1356036198452 1356039026690 1356039067568 1356035166411 

In my case, there can be from 1 to 100 timestamps.

So the fastest way to get to the β€œlatest” date (in my opinion) would be to do this:

 var newestDate = Math.max(1356036198452,1356039026690,1356039067568,1356035166411); alert(newestDate); 

Hope someone helps in the real world.

+1
source

Source: https://habr.com/ru/post/1344361/


All Articles