Skip to content

JavaScript Date(日期)

什么是 Date 对象

Date 对象是 JavaScript 中用于处理日期和时间的内置对象。它允许你创建、操作和格式化日期和时间值。

创建 Date 对象

你可以通过以下方式创建 Date 对象:

javascript
// 创建当前日期和时间
let now = new Date();
console.log(now); // 例如:2023-10-05T14:48:00.000Z

// 通过指定日期字符串创建
let date1 = new Date("2023-10-05");
console.log(date1); // 2023-10-05T00:00:00.000Z

// 通过指定年、月、日创建(月份从0开始,0-11)
let date2 = new Date(2023, 9, 5); // 注意:10月是9
console.log(date2); // 2023-10-05T00:00:00.000Z

// 通过指定年、月、日、时、分、秒创建
let date3 = new Date(2023, 9, 5, 10, 30, 45);
console.log(date3); // 2023-10-05T10:30:45.000Z

// 通过时间戳创建(从1970年1月1日00:00:00 UTC开始的毫秒数)
let date4 = new Date(1696492800000);
console.log(date4); // 2023-10-05T00:00:00.000Z

Date 对象的方法

获取日期和时间组件

javascript
let date = new Date(2023, 9, 5, 10, 30, 45, 123);

// 获取年份
console.log(date.getFullYear()); // 2023

// 获取月份(0-11)
console.log(date.getMonth()); // 9 (10月)

// 获取日期(1-31)
console.log(date.getDate()); // 5

// 获取星期几(0-6,0表示星期日)
console.log(date.getDay()); // 4 (星期四)

// 获取小时(0-23)
console.log(date.getHours()); // 10

// 获取分钟(0-59)
console.log(date.getMinutes()); // 30

// 获取秒数(0-59)
console.log(date.getSeconds()); // 45

// 获取毫秒数(0-999)
console.log(date.getMilliseconds()); // 123

// 获取时间戳(从1970年1月1日00:00:00 UTC开始的毫秒数)
console.log(date.getTime()); // 1696530645123

设置日期和时间组件

javascript
let date = new Date();

// 设置年份
date.setFullYear(2024);

// 设置月份(0-11)
date.setMonth(0); // 1月

// 设置日期(1-31)
date.setDate(1);

// 设置小时(0-23)
date.setHours(12);

// 设置分钟(0-59)
date.setMinutes(0);

// 设置秒数(0-59)
date.setSeconds(0);

// 设置毫秒数(0-999)
date.setMilliseconds(0);

// 设置时间戳
date.setTime(1704067200000); // 2024-01-01T12:00:00.000Z

console.log(date); // 2024-01-01T12:00:00.000Z

UTC 方法

Date 对象还提供了一系列以 UTC 为基础的方法,用于处理 UTC 时间:

javascript
let date = new Date(2023, 9, 5, 10, 30, 45);

// 获取 UTC 年份
console.log(date.getUTCFullYear()); // 2023

// 获取 UTC 月份(0-11)
console.log(date.getUTCMonth()); // 9

// 获取 UTC 日期(1-31)
console.log(date.getUTCDate()); // 5

// 获取 UTC 小时(0-23)
console.log(date.getUTCHours()); // 10

// 其他 UTC 方法类似...

日期格式化

toString() 方法

javascript
let date = new Date(2023, 9, 5, 10, 30, 45);

// 转换为字符串
console.log(date.toString()); // Thu Oct 05 2023 10:30:45 GMT+0800 (中国标准时间)

// 转换为 UTC 字符串
console.log(date.toUTCString()); // Thu, 05 Oct 2023 02:30:45 GMT

// 转换为 ISO 字符串
console.log(date.toISOString()); // 2023-10-05T02:30:45.000Z

// 转换为日期字符串
console.log(date.toDateString()); // Thu Oct 05 2023

// 转换为时间字符串
console.log(date.toTimeString()); // 10:30:45 GMT+0800 (中国标准时间)

自定义格式化

javascript
function formatDate(date) {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  const hours = String(date.getHours()).padStart(2, "0");
  const minutes = String(date.getMinutes()).padStart(2, "0");
  const seconds = String(date.getSeconds()).padStart(2, "0");

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

let date = new Date();
console.log(formatDate(date)); // 例如:2023-10-05 10:30:45

日期计算

计算两个日期之间的差值

javascript
let date1 = new Date(2023, 9, 1);
let date2 = new Date(2023, 9, 5);

// 计算毫秒差
let diffMs = date2 - date1;
console.log(diffMs); // 345600000 (4天 * 24小时 * 60分钟 * 60秒 * 1000毫秒)

// 转换为天数
let diffDays = diffMs / (1000 * 60 * 60 * 24);
console.log(diffDays); // 4

// 转换为小时
let diffHours = diffMs / (1000 * 60 * 60);
console.log(diffHours); // 96

日期的增减

javascript
let date = new Date(2023, 9, 5);

// 增加一天
date.setDate(date.getDate() + 1);
console.log(date); // 2023-10-06

// 减少一天
date.setDate(date.getDate() - 1);
console.log(date); // 2023-10-05

// 增加一个月
date.setMonth(date.getMonth() + 1);
console.log(date); // 2023-11-05

// 减少一个月
date.setMonth(date.getMonth() - 1);
console.log(date); // 2023-10-05

// 增加一年
date.setFullYear(date.getFullYear() + 1);
console.log(date); // 2024-10-05

// 减少一年
date.setFullYear(date.getFullYear() - 1);
console.log(date); // 2023-10-05

日期比较

javascript
let date1 = new Date(2023, 9, 1);
let date2 = new Date(2023, 9, 5);
let date3 = new Date(2023, 9, 1);

// 比较日期大小
console.log(date1 < date2); // true
console.log(date1 > date2); // false
console.log(date1 === date3); // false(对象引用比较)

// 正确的比较方式(使用时间戳)
console.log(date1.getTime() === date3.getTime()); // true

最佳实践

  1. 注意月份的索引:JavaScript 的月份是从 0 开始的(0-11),所以 10 月对应的是 9。

  2. 处理时区问题

    • getFullYear(), getMonth(), getDate() 等方法返回的是本地时区的日期和时间
    • getUTCFullYear(), getUTCMonth(), getUTCDate() 等方法返回的是 UTC 时区的日期和时间
    • 当需要处理跨时区的日期时,建议使用 UTC 方法
  3. 避免直接比较 Date 对象:Date 对象是引用类型,直接使用 === 比较的是对象引用,而不是日期值。应该使用 getTime() 方法获取时间戳后再比较。

  4. 注意日期计算的边界情况

    • 月份的天数不同(2 月可能有 28 或 29 天)
    • 闰年的处理
    • 夏令时的影响
  5. 使用第三方库处理复杂日期操作:对于复杂的日期操作(如日历计算、时区转换等),考虑使用 Moment.js、date-fns 或 Luxon 等第三方库。

  6. 性能考虑:频繁创建 Date 对象可能会影响性能,特别是在循环中。如果需要多次使用当前时间,建议只创建一次 Date 对象并重用。

  7. 格式化日期时的兼容性:不同浏览器对日期字符串的解析可能存在差异,特别是对于非标准格式的日期字符串。建议使用标准的 ISO 8601 格式(如 '2023-10-05')或使用 new Date(year, month, day) 构造函数。

总结

Date 对象是 JavaScript 中处理日期和时间的核心对象,它提供了丰富的方法来创建、操作和格式化日期和时间。了解 Date 对象的特性和用法,有助于你更有效地处理 JavaScript 中的日期和时间相关任务。

通过合理使用 Date 对象的方法和遵循最佳实践,你可以避免常见的日期处理错误,编写更可靠的 JavaScript 代码来处理各种日期和时间操作。