一、日期对象基础操作
1.1 创建日期实例
JavaScript通过Date构造函数创建日期对象,支持多种参数形式:
// 当前时间const now = new Date();// 指定日期时间(年,月-1,日,时,分,秒)const specificDate = new Date(2025, 7, 3, 18, 30, 0);// ISO字符串格式const isoDate = new Date('2025-08-03T18:30:00');
注意:月份参数从0开始计数(0=1月),这种设计源于早期编程语言的约定,需特别注意。
1.2 获取日期组件
通过日期对象的方法可提取具体时间单位:
const date = new Date();const components = {year: date.getFullYear(), // 2025month: date.getMonth() + 1, // 8(补正月份偏移)day: date.getDate(), // 3weekday: date.getDay(), // 0-6(0=周日)hours: date.getHours(), // 18minutes: date.getMinutes(), // 30seconds: date.getSeconds(), // 0milliseconds: date.getMilliseconds() // 毫秒};
最佳实践:建议将获取的月份+1后再使用,避免逻辑错误。
二、时间戳操作
2.1 获取当前时间戳
时间戳表示自1970-01-01 00:00:00 UTC以来的毫秒数:
// 方法1:Date.now()(推荐)const timestamp1 = Date.now();// 方法2:new Date().getTime()const timestamp2 = new Date().getTime();// 方法3:+new Date()(隐式转换)const timestamp3 = +new Date();
性能对比:Date.now()比new Date().getTime()快约30%,在高频调用场景建议使用。
2.2 时间戳转换
// 时间戳转日期对象const dateFromTimestamp = new Date(1754262080123);// 日期对象转本地格式字符串console.log(dateFromTimestamp.toLocaleString());// 输出示例:2025/8/3 下午2:01:20(根据系统区域设置变化)
三、日期运算
3.1 基本加减运算
通过修改时间戳实现日期加减:
const now = new Date();// 加1天(24*60*60*1000毫秒)const tomorrow = new Date(now.getTime() + 86400000);// 减2小时const twoHoursAgo = new Date(now.getTime() - 2*60*60*1000);// 加30分钟const later = new Date(now.getTime() + 30*60*1000);
进阶技巧:对于跨月/跨年运算,建议使用setDate()等内置方法:
// 加1个月(自动处理月份溢出)const nextMonth = new Date(now);nextMonth.setMonth(now.getMonth() + 1);
3.2 日期差值计算
计算两个日期的间隔天数:
function getDaysBetween(date1, date2) {const diffTime = Math.abs(date2 - date1);return Math.ceil(diffTime / (1000 * 60 * 60 * 24));}const start = new Date('2025-08-01');const end = new Date('2025-08-03');console.log(getDaysBetween(start, end)); // 2
四、日期格式化
4.1 基础格式化函数
实现YYYY-MM-DD HH格式:
ss
function formatDate(date) {const pad = num => String(num).padStart(2, '0');return [date.getFullYear(),pad(date.getMonth() + 1),pad(date.getDate())].join('-') + ' ' + [pad(date.getHours()),pad(date.getMinutes()),pad(date.getSeconds())].join(':');}console.log(formatDate(new Date()));// 示例输出:2025-08-03 18:30:00
4.2 使用Intl.DateTimeFormat
现代浏览器支持国际化API:
const formatter = new Intl.DateTimeFormat('zh-CN', {year: 'numeric',month: '2-digit',day: '2-digit',hour: '2-digit',minute: '2-digit',second: '2-digit',hour12: false});console.log(formatter.format(new Date()));// 示例输出:2025/08/03 18:30:00
五、日期比较与验证
5.1 比较操作
const date1 = new Date('2025-08-01');const date2 = new Date('2025-08-03');// 直接比较(基于时间戳)if (date1 < date2) console.log('date1早于date2');// 判断是否为有效日期function isValidDate(date) {return date instanceof Date && !isNaN(date);}console.log(isValidDate(new Date('invalid'))); // false
5.2 时区处理
// 获取UTC时间const utcDate = new Date();console.log(utcDate.toUTCString());// 示例输出:Sun, 03 Aug 2025 10:30:00 GMT// 获取本地时区偏移(分钟)const timezoneOffset = new Date().getTimezoneOffset();console.log(`时区偏移:${timezoneOffset/60}小时`);
六、实际应用场景
6.1 倒计时实现
function startCountdown(targetDate) {const timer = setInterval(() => {const now = new Date();const diff = targetDate - now;if (diff <= 0) {clearInterval(timer);console.log('倒计时结束');return;}const days = Math.floor(diff / (1000*60*60*24));const hours = Math.floor((diff % (1000*60*60*24)) / (1000*60*60));// ...继续计算分钟秒console.log(`剩余${days}天${hours}小时`);}, 1000);}const futureDate = new Date();futureDate.setDate(futureDate.getDate() + 7);startCountdown(futureDate);
6.2 日历组件开发
// 获取某月的第一天和天数function getMonthInfo(year, month) {const firstDay = new Date(year, month-1, 1);const lastDay = new Date(year, month, 0);return {firstDay: firstDay.getDay(), // 周几daysCount: lastDay.getDate() // 天数};}console.log(getMonthInfo(2025, 8));// 输出:{firstDay: 6, daysCount: 31}(8月1日是周六)
七、性能优化建议
- 缓存日期对象:频繁操作时避免重复创建对象
- 使用时间戳运算:比日期对象加减更高效
- 减少格式化操作:只在最终显示时格式化
- 考虑使用库:复杂场景可引入
dayjs或date-fns等轻量库
八、常见问题解决方案
8.1 月份错位问题
始终记住月份从0开始,建议封装辅助函数:
function getMonth(date, offset=0) {return date.getMonth() + 1 + offset;}
8.2 时区陷阱
避免直接拼接日期字符串,推荐使用ISO格式:
// 错误示范(依赖浏览器时区解析)new Date('2025-08-03');// 正确做法(明确指定时区)new Date('2025-08-03T00:00:00Z'); // UTC时间
8.3 闰秒处理
JavaScript日期对象自动处理闰秒,开发者无需特殊处理。
通过系统掌握这些核心操作,开发者可以高效处理各种日期时间场景。对于企业级应用,建议结合监控告警系统记录日期操作的关键节点,确保业务逻辑的可靠性。在分布式系统中,所有时间相关操作都应使用服务器时间而非客户端时间,避免时区不一致导致的问题。