0

I need to compare 2 UNIX timestamps, one of them is token expire time (in future) and another is Date.now(). By some reason by comparing 2 timestamps it returns false result. It returns that validUntil is less than Date.now(). I cannot get why, googled but have not found any info.

var validUntil = 1629361800

// if validUntil is less than now, then token is expired

validUntil < Date.now()

// returns false, however, validUntil is greater than Date.now()
Anna
  • 914
  • 9
  • 25
  • I am unable to understand your question, isn't that is how `<` symbol supposed to work since `validUntil` is greater than `Date.now()` – Sumit Surana Aug 19 '21 at 07:52
  • the misunderstanding is that `Date.now()` is a *unix timestamp* - when it isn't – Bravo Aug 19 '21 at 07:56

2 Answers2

4

Date.now() returns number of milliseconds, not seconds... so you need to divide it by 1000 or multiply the other one by 1000.

var validUntil = 1629361800
validUntil * 1000 < Date.now()
Flash Thunder
  • 11,672
  • 8
  • 47
  • 91
0

Date.now() the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC, and your validUntil looks like a second number

sunzheng04
  • 29
  • 2