BaconJuice
BaconJuice

Reputation: 3769

Some explanations on Date.now

Could someone explain to me what this returning number means? and how it is derived to that?

console.log(Date.now() - 24 * 60 * 60 * 1000);

If I wanted to use the above formula to display the next 15minutes and not 24 hours? how would I alter it?

Upvotes: 0

Views: 323

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340723

Date.now() returns:

the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.

24 * 60 * 60 * 1000 in milliseconds represents 24 hours*. So you basically get a timestamp 24 hours in the past from now. Notice that due to DST this doesn't necessarily compute a timestamp one day in the past. It's 24 hours in the past.

Also to get some meaningful output you should wrap resulting number in Date:

console.log(new Date(Date.now() - 24 * 60 * 60 * 1000));

Finally Date.now() can be replaced with new Date() when using in arithmetic expression.

* - 24 (hours) times 60 (minutes in hour) times 60 (seconds in minute) times 1000 milliseconds in second.

Upvotes: 3

Related Questions