Jump to content

JavaScript/Dates/Exercises

From Wikibooks, open books for an open world

1. You can show the offset between your timezone and UTC by using getTimezoneOffset(). Show this offset.

Click to see solution
"use strict";
const ts_1 = new Date();
alert(ts_1.getTimezoneOffset());

2. Show the offset again, but don't use getTimezoneOffset(). Instead, work with Date.now() and Date.UTC().

Click to see solution
"use strict";

// Use an arbitrary timestamp, i.e., the first second of our century.
// a) in your timezone
const ts_2 = new Date(2000, 0, 1);
alert('Numerical value: ' + ts_2.valueOf());
alert(new Date(ts_2));

// b) in UTC
const ts_3 = Date.UTC(2000, 0, 1);
alert('Numerical value: ' + ts_3.valueOf());
alert(new Date(ts_3));

// the difference
const minutes = (ts_2 - ts_3) / 60000;
alert(minutes);