网站
引入
在网页开发中,经常需要处理时间相关的功能,比如显示当前时间、倒计时、记录用户操作时间、生日提醒等。JS 本身没有专门的日期数据类型,而是通过 Date 对象来保存和操作日期时间。
Date 对象用来表示一个具体的时间点,可以获取年、月、日、时、分、秒,也可以做时间计算和格式化。
正文
定义
定义
Date对象是 JS 内置的对象,用于保存和操作一个日期时间。一个Date实例表示从 1970 年 1 月 1 日 00:00:00(UTC)开始经过的毫秒数。
创建 Date 对象
获取当前时间
最常见的方式是不传参数,得到当前时间。
let now = new Date();
console.log(now);指定日期时间
可以传入字符串或数字来创建指定时间。
let birthday = new Date("2000-05-20");
console.log(birthday);也可以按年、月、日、时、分、秒、毫秒的顺序传入数字。
let day = new Date(2024, 4, 20, 10, 30, 0);
console.log(day);注意:月份从 0 开始,4 表示 5 月。
获取日期时间信息
Date 对象提供了一系列 get 方法,用来读取时间各部分。
let now = new Date();
console.log(now.getFullYear());
console.log(now.getMonth());
console.log(now.getDate());
console.log(now.getDay());
console.log(now.getHours());
console.log(now.getMinutes());
console.log(now.getSeconds());
console.log(now.getMilliseconds());
console.log(now.getTime());| 方法 | 作用 |
|---|---|
getFullYear() | 获取四位数的年份 |
getMonth() | 获取月份,0 到 11 |
getDate() | 获取一个月中的某一天,1 到 31 |
getDay() | 获取星期几,0(周日)到 6(周六) |
getHours() | 获取小时,0 到 23 |
getMinutes() | 获取分钟,0 到 59 |
getSeconds() | 获取秒,0 到 59 |
getMilliseconds() | 获取毫秒,0 到 999 |
getTime() | 获取从 1970 年 1 月 1 日到现在的毫秒数 |
设置日期时间信息
Date 对象也提供了对应的 set 方法,用来修改时间各部分。
let day = new Date();
day.setFullYear(2025);
day.setMonth(11);
day.setDate(31);
day.setHours(23);
day.setMinutes(59);
day.setSeconds(0);
console.log(day);| 方法 | 作用 |
|---|---|
setFullYear(year) | 设置年份 |
setMonth(month) | 设置月份,从 0 开始 |
setDate(day) | 设置一个月中的某一天 |
setHours(hour) | 设置小时 |
setMinutes(min) | 设置分钟 |
setSeconds(sec) | 设置秒 |
setMilliseconds(ms) | 设置毫秒 |
setTime(ms) | 用毫秒数设置时间 |
日期格式化
toISOString()
返回 ISO 格式的字符串,常用在数据传输中。
let now = new Date();
console.log(now.toISOString());toLocaleDateString()
返回本地格式的日期字符串。
let now = new Date();
console.log(now.toLocaleDateString());toLocaleTimeString()
返回本地格式的时间字符串。
let now = new Date();
console.log(now.toLocaleTimeString());toLocaleString()
同时返回日期和时间。
let now = new Date();
console.log(now.toLocaleString());以上方法都可以传入地区参数调整显示格式。
let now = new Date();
console.log(now.toLocaleString("zh-CN"));
console.log(now.toLocaleString("en-US"));格式化例子
完整展示几种格式化方法的输出结果。
let now = new Date("2026-06-27T10:30:00");
// ISO 格式,常用于数据传输
console.log(now.toISOString());
// 2026-06-27T10:30:00.000Z
// 本地日期
console.log(now.toLocaleDateString("zh-CN"));
// 2026/6/27
// 本地时间
console.log(now.toLocaleTimeString("zh-CN"));
// 10:30:00
// 本地日期时间
console.log(now.toLocaleString("zh-CN"));
// 2026/6/27 10:30:00
// 英文格式
console.log(now.toLocaleString("en-US"));
// 6/27/2026, 10:30:00 AM也可以通过配置对象自定义显示样式。
let now = new Date("2026-06-27T10:30:00");
let options = {
year: "numeric",
month: "long",
day: "numeric",
hour: "2-digit",
minute: "2-digit"
};
console.log(now.toLocaleString("zh-CN", options));
// 2026年6月27日 10:30时间戳
时间戳是从 1970 年 1 月 1 日 00:00:00(UTC)到某个时间点经过的毫秒数,常用来精确记录和比较时间。
获取时间戳
let now = new Date();
console.log(now.getTime());也可以使用 Date.now() 直接获取当前时间戳,不需要创建对象。
console.log(Date.now());用时间戳创建 Date
let timestamp = 1719504000000;
let day = new Date(timestamp);
console.log(day);时间计算
Date 对象相减会自动转成毫秒数,可以用来做时间差计算。
计算两个日期相差天数
let start = new Date("2024-01-01");
let end = new Date("2024-01-10");
let diff = end - start;
let days = diff / (1000 * 60 * 60 * 24);
console.log(days);1000 * 60 * 60 * 24 表示一天的毫秒数。
几天后的日期
let now = new Date();
let sevenDaysLater = new Date(now.getTime() + 7 * 1000 * 60 * 60 * 24);
console.log(sevenDaysLater);也可以直接用 setDate() 计算。
let now = new Date();
now.setDate(now.getDate() + 7);
console.log(now);例子
显示当前时间
function showTime() {
let now = new Date();
let year = now.getFullYear();
let month = now.getMonth() + 1;
let day = now.getDate();
let hour = now.getHours();
let minute = now.getMinutes();
let second = now.getSeconds();
let time = year + "-" +
String(month).padStart(2, "0") + "-" +
String(day).padStart(2, "0") + " " +
String(hour).padStart(2, "0") + ":" +
String(minute).padStart(2, "0") + ":" +
String(second).padStart(2, "0");
console.log(time);
}
showTime();注意:getMonth() 返回 0 到 11,显示时要 +1。
倒计时
function countdown(target) {
let now = new Date();
let end = new Date(target);
let diff = end - now;
let days = Math.floor(diff / (1000 * 60 * 60 * 24));
let hours = Math.floor((diff / (1000 * 60 * 60)) % 24);
let minutes = Math.floor((diff / (1000 * 60)) % 60);
let seconds = Math.floor((diff / 1000) % 60);
console.log(days + "天 " + hours + "时 " + minutes + "分 " + seconds + "秒");
}
countdown("2026-12-31 23:59:59");计算年龄
function getAge(birthday) {
let birth = new Date(birthday);
let now = new Date();
let age = now.getFullYear() - birth.getFullYear();
let monthDiff = now.getMonth() - birth.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && now.getDate() < birth.getDate())) {
age--;
}
return age;
}
console.log(getAge("2000-05-20"));判断闰年
function isLeapYear(year) {
let date = new Date(year, 1, 29);
return date.getMonth() === 1;
}
console.log(isLeapYear(2024));
console.log(isLeapYear(2023));思路是构造 2 月 29 日,如果不是闰年,日期会自动进位到 3 月 1 日,getMonth() 就不再是 1。
特点
- 创建
Date对象必须使用new Date() - 月份从
0开始,0表示 1 月,11表示 12 月 - 星期从
0开始,0表示周日 getTime()和Date.now()都返回毫秒数,常用于比较和计算时间差get方法读取时间,set方法修改时间,方法名成对出现- 两个
Date对象相减得到毫秒数,方便做时间计算 toLocaleString()会根据系统地区显示不同格式Date对象是可变的,set方法会直接修改原对象- 时间戳常用于记录操作时间、生成唯一标识、计算耗时
理解
可以把 Date 对象理解为一个保存了某个时间点的容器。它内部记录的是一串毫秒数,所有 get 和 set 方法都只是把这串毫秒数拆成年月日时分秒方便阅读和操作。
月份从 0 开始是最容易踩坑的地方,写代码时要记得 +1。
时间戳这个概念也很重要,它是比较时间、计算时间差、记录操作时间的最直接方式。
实际开发中,Date 对象常和 JS 定时器 配合使用,实现时钟、倒计时、定时刷新等功能;也可以结合 JS DOM API 把时间显示到页面上。
引出
理解了 Date 对象之后,可以继续学习 JS Math 对象,了解 JS 里数学计算相关的内置对象。
后续也可以结合 JS 定时器 和 JS DOM API,实现时钟、倒计时、定时刷新等交互功能。