9

I want to set the date as an infinite date in form of timestamp? There are people saying we can use "0" and some are saying set big value like this 60*60*24*100. And I do not want to do this. Is there any suitable way of handling this?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Dinnu Buddy
  • 369
  • 1
  • 3
  • 13

1 Answers1

21

0 and 60*60*24*100 are bad choices because they are small values (in the first few hours of 1st January 1970).

If you need to deal with the timestamps as a number only, use Infinity. It has the properties I assume you need:

Infinity > Date.now()
// true

Infinity > 60*60*24*100
// true

Infinity > Number.MAX_VALUE
// true

However, it won't work if you need to convert to Date:

new Date(Infinity)
// Invalid Date

If you also need to construct a Date for your timestamp, the largest possible timestamp is 8640000000000000 - see Minimum and maximum date.

var MAX_TIMESTAMP = 8640000000000000;

new Date(MAX_TIMESTAMP)
// Sat Sep 13 275760 01:00:00 GMT+0100 (BST)

new Date(MAX_TIMESTAMP + 1)
// Invalid Date
Community
  • 1
  • 1
joews
  • 29,767
  • 10
  • 79
  • 91