跳轉到內容

JavaScript/日期/練習

來自: Wikibooks,開放的世界開放的書籍

1. 您可以使用 getTimezoneOffset() 檢視您所在時區與 UTC 之間的偏差。顯示該偏差。

單擊檢視解決方案
"use strict";
const ts_1 = new Date();
alert(ts_1.getTimezoneOffset());

2. 再次顯示該偏差,但不要使用 getTimezoneOffset()。相反,使用 Date.now() 和 Date.UTC()。

單擊檢視解決方案
"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);
華夏公益教科書