Dates and Timestamps
new Date() is the current moment, as an object. getHours(), getMinutes() and getSeconds() read its parts in the device's own time zone.
Date.now() is the same moment as a single number: the milliseconds since the first of January 1970. That number is called a timestamp, and it makes time arithmetic simple. Five minutes from now is Date.now() + 5 * 60 * 1000, and the gap between two moments is one subtraction.
A clock shows 09:05:07, not 9:5:7, so every number needs two digits. String(n).padStart(2, "0") adds zeros on the left until the text is two characters long. To turn a number of seconds into a display, split it up: Math.floor(s / 3600) hours, Math.floor(s % 3600 / 60) minutes, and s % 60 seconds.
Mark this lesson complete
Signed in, your progress follows you to every device.