In the previous lessons, we used the timestamp
format to find the difference between dates.
However, this is not really necessary in
JavaScript: dates represented as a Date
object can be subtracted from each other, and
the result of their subtraction is the
difference in milliseconds.
For example, let's print the number of milliseconds
that have passed since May 25
,
2015
, 12:59:59
until now:
let now = new Date();
let date = new Date(2015, 4, 25, 12, 59, 59);
let diff = now - date; // subtracts two objects with dates from each other
console.log(diff); // shows the difference in milliseconds
Print the number of milliseconds elapsed
between September 1
, 2000
and February 15
, 2010
.
Modify the previous task to display the difference in days.
Modify the previous task to display the difference in months.
Modify the previous task to display the difference in years.