This blog post explains how JavaScript dates are stored internally as time values, milliseconds since 1970-01-01.
What the date API calls time is called a time value by the ECMAScript specification. It is a primitive number that encodes a date as milliseconds since 1 January 1970 00:00:00 UTC. Each date object stores its state as a time value, in the internal property [[PrimitiveValue]] (the same property that instances of the wrapper constructors Boolean, Number, and String use to store their wrapped primitive values).
Warning: Leap seconds are ignored in time values.
The following methods work with time values:
A few examples of converting dates to time values:
> new Date('1970-01-01').getTime() 0 > new Date('1970-01-02').getTime() 86400000 > new Date('1960-01-02').getTime() -315532800000The Date constructor enables you to convert times values to dates:
> new Date(0) Date {Thu Jan 01 1970 01:00:00 GMT+0100 (CET)} > new Date(24 * 60 * 60 * 1000) // 1 day in ms Date {Fri Jan 02 1970 01:00:00 GMT+0100 (CET)} > new Date(-315532800000) Date {Sat Jan 02 1960 01:00:00 GMT+0100 (CET)}
> new Date('1980-05-21') > new Date('1980-05-20') trueYou can also perform arithmetic, but beware that leap seconds are ignored:
> new Date('1980-05-21') - new Date('1980-05-20') 86400000