4.4 日期時間(Date)
在 javascript/practice
資料夾下,建立 date.html
來練習。
建立當下的日期物件
預設上會抓取瀏覽器上所設定的時區。建立當下的日期物件:
var d = new Date(); // 建立一個日期物件
console.log(d);
// Sun Aug 02 2020 11:48:41 GMT+0800 (Taipei Standard Time)
指定日期時間的日期物件
共有7個參數,分別是年、月、日、時、分、秒、毫秒。
留意「月」的部份,0 是一月份;到 11 是十二月份。
例 1:(指定到 7 個參數(
var d1 = new Date(2020, 7, 2, 12, 48, 10, 0);
var d2 = new Date(2020, 7, 2, 12, 48, 10, 1000);
console.log(d1); // Sun Aug 02 2020 12:48:10 GMT+0800 (Taipei Standard Time)
console.log(d2); // Sun Aug 02 2020 12:48:11 GMT+0800 (Taipei Standard Time)
例 2:(只指定 2 ~ 6 個參數)
var d1 = new Date(2020, 7, 2, 12, 48, 10);
var d2 = new Date(2020, 7, 2, 12, 48);
var d3 = new Date(2020, 7, 2, 12);
var d4 = new Date(2020, 7, 2);
var d5 = new Date(2020, 7);
console.log(d1); // Sun Aug 02 2020 12:48:10 GMT+0800 (Taipei Standard Time)
console.log(d2); // Sun Aug 02 2020 12:48:00 GMT+0800 (Taipei Standard Time)
console.log(d3); // Sun Aug 02 2020 12:00:00 GMT+0800 (Taipei Standard Time)
console.log(d4); // Sun Aug 02 2020 00:00:00 GMT+0800 (Taipei Standard Time)
console.log(d5); // Sat Aug 01 2020 00:00:00 GMT+0800 (Taipei Standard Time)
例 3:(指定 1 個參數,指的是毫秒)
var d1 = new Date(0);
var d2 = new Date(1000); // 代入的是「毫秒」,即視為第7個參數
console.log(d1); // Thu Jan 01 1970 08:00:00 GMT+0800 (Taipei Standard Time)
console.log(d2); // Thu Jan 01 1970 08:00:01 GMT+0800 (Taipei Standard Time)
指定日期字串的日期物件
YYYY-MM-DD HH:MM:SS
var d1 = new Date("2020-08-02");
var d2 = new Date("2020-08-02 13:40:10");
var d3 = new Date("2020-08");
console.log(d1); // Sun Aug 02 2020 08:00:00 GMT+0800 (Taipei Standard Time)
console.log(d2); // Sun Aug 02 2020 13:40:10 GMT+0800 (Taipei Standard Time)
console.log(d3); // Sat Aug 01 2020 08:00:00 GMT+0800 (Taipei Standard Time)
YYYY-MM-DDTHH:MM:SSZ
T
:大寫的 T,用來做為日期與時間的區隔。Z
:表示的是 UTC 時區。如果想要表達相對於 UTC 時區,就將
Z
移除,改成+HH:MM
或-HH:MM
。
例 1:(留意輸出會是 +8
的時區)
var d1 = new Date("2020-08-02T13:10:00Z");
console.log(d1); // Sun Aug 02 2020 21:10:00 GMT+0800 (Taipei Standard Time)
例 2:(改成 +8
時區)
var d1 = new Date("2020-08-02T13:10:00Z");
var d2 = new Date("2020-08-02T13:10:00+08:00");
console.log(d1); // Sun Aug 02 2020 21:10:00 GMT+0800 (Taipei Standard Time)
console.log(d2); // Sun Aug 02 2020 13:10:00 GMT+0800 (Taipei Standard Time)
GMT 與 UTC 查詢參考:

日期相關操作
一旦日期物件建立了,那就可以使用以下相關操作:
toString()
將日期物件的輸出轉成字串:
var d1 = new Date();
console.log(d1.toString()); // Sun Aug 02 2020 13:57:50 GMT+0800 (Taipei Standard Time)
取得日期資訊
方法
說明
getFullYear()
取得四位數的西元年。
getMonth()
0 ~ 11。取得月份。0 代表一月,11 代表十二月。
getDate()
1 ~ 31。取得日期。
getHours()
0 ~ 23。取得小時的部份。
getMinutes()
0 ~ 59。取得分鐘。
getSeconds()
0 ~ 59。取得秒數。
getMilliseconds()
0 ~ 999。取得毫秒數。
getTime()
取得 Unix Timestamp,回傳毫秒數。
getDay()
0 ~ 6。取得星期幾。0 代表星期日,6 代表星期六。
Date.now()
與 getTime() 同。
例:
var d1 = new Date();
console.log(d1.getFullYear()); // 2020
console.log(d1.getMonth()); // 7
console.log(d1.getDate()); // 2
console.log(d1.getHours()); // 14
console.log(d1.getMinutes()); // 21
console.log(d1.getSeconds()); // 0
console.log(d1.getMilliseconds()); // 518
console.log(d1.getTime()); // 1596349260518
console.log(d1.getDay()); // 0
console.log(Date.now()); // 1596349260518
其它
1 天 = 24 小時 = 60 * 60 * 24 = 86400 秒 = 86400000 毫秒。
練習
完成以下函式,然後在 console 中輸出的結果是差「幾」天:
function DateDiff(date1, date2) {
}
console.log( DateDiff("2019-12-31", "2020-01-02") ); // 2
console.log( DateDiff("2019-12-30", "2020-01-03") ); // 4
console.log( DateDiff("2019-12-30", "2019-12-30") ); // 0
參考作法:
Last updated