I understand that an ECMAScript date just stores a time value in epoch time (the number of milliseconds since midnight on 1 January 1970 UTC).
But looking over the algorithm for the Date() constructor, I only see where the argument values are converted to milliseconds; I don't see where the millisecond value is converted to epoch time. Where does that step happen?
The Date constructor algorithm calls MakeDay, which returns the number of days since epoch for the argument values. MakeDate then does convert that number of days to the number milliseconds by simple multiplication.
MakeDay contains the following step:
- Find a finite time value
tsuch that YearFromTime(t) isymand MonthFromTime(t) ismnand DateFromTime(t) is 1๐ฝ;
This is a rather obscure definition, using the operations defined in 21.4.1.3 "Year Number". There we can find the formula
DayFromYear(y) = ๐ฝ(365 ร (โ(y) - 1970) + floor((โ(y) - 1969) / 4) - floor((โ(y) - 1901) / 100) + floor((โ(y) - 1601) / 400))
This comes out to have "day 0" as the first day of 1970.
But rather than deriving that from this formula, we can just read 21.4.1.1 "Time Values and Time Range":
The exact moment of midnight at the beginning of 1 January 1970 UTC is represented by the time value
+0๐ฝ.