1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
// 取當前日期時間(timestamp 時間戳)
const timestamp = Date.now();
console.log(timestamp); // 1713251128711
// 取當前日期時間(ISO 8601 的格式)
const nowDateTimeISO = new Date().toISOString().split(".")[0] + "Z";
console.log(nowDateTimeISO); // 2024-04-16T07:17:31Z
// 取當前日期時間(補零)
const nowDateTime = new Date(Date.now()).toLocaleString("sv");
console.log(nowDateTime); // 2024-04-16 13:51:58
// 取當前日期時間(補零,指定時區)
const nowDateTimeNY = new Date(Date.now()).toLocaleString("sv", {
timeZone: "America/New_York"
});
console.log(nowDateTimeNY); // 2024-04-16 03:05:02
// 取當前日期(補零)
const nowDate = new Date(Date.now()).toLocaleString("sv").split(" ")[0];
console.log(nowDate); // 2024-04-16
// 取當前時間(補零)
const nowTime = new Date(Date.now()).toLocaleString("sv").split(" ")[1];
console.log(nowTime); // 13:55:08
|