日期与时间基础
介绍 Date
的创建、解析、常用方法与时区/本地化显示。
创建与获取
js
const now = new Date(); // 当前时间
const fromTs = new Date(1726500000000); // 通过时间戳
const fromStr = new Date("2025-08-25T00:00:00+08:00"); // ISO 字符串
now.getFullYear(); // 年
now.getMonth(); // 月(0-11)
now.getDate(); // 日(1-31)
now.getHours(); // 时(0-23)
now.getMinutes(); // 分(0-59)
now.getSeconds(); // 秒(0-59)
now.getTime(); // 时间戳(ms)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
格式化显示
js
// 简单:内置 toLocaleString
now.toLocaleString("zh-CN", { hour12: false });
// 更细控制:Intl.DateTimeFormat
const fmt = new Intl.DateTimeFormat("zh-CN", {
year: "numeric", month: "2-digit", day: "2-digit",
hour: "2-digit", minute: "2-digit", second: "2-digit",
hour12: false,
});
fmt.format(now);
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
时间计算
js
// 加减毫秒
const after5m = new Date(now.getTime() + 5 * 60 * 1000);
// 计算两个时间差(秒)
function diffSeconds(a, b) {
return Math.abs(a.getTime() - b.getTime()) / 1000;
}
1
2
3
4
5
6
7
2
3
4
5
6
7