Date对象

Last updated: ... / Reads: 45 Edit

JavaScript 的Date对象是用于处理日期和时间的内置对象。它允许您创建一个表示特定日期和时间的对象,并提供了各种方法来操作和获取日期时间的各个部分。

下面是一些常见的Date对象的用法示例:

  1. 创建一个表示当前日期和时间的Date对象:
const currentDate = new Date();
console.log(currentDate);
  1. 创建一个特定日期和时间的Date对象:
const specificDate = new Date('2023-11-11T13:36:53');
console.log(specificDate);
  1. 获取日期时间的各个部分:
const date = new Date();
console.log(date.getFullYear());  // 获取年份
console.log(date.getMonth());     // 获取月份(0-11,0 表示一月)
console.log(date.getDate());      // 获取日期(1-31)
console.log(date.getHours());     // 获取小时(0-23)
console.log(date.getMinutes());   // 获取分钟(0-59)
console.log(date.getSeconds());   // 获取秒数(0-59)
console.log(date.getMilliseconds());  // 获取毫秒数
console.log(date.getDay());       // 获取星期几(0-6,0 表示星期日)
  1. 格式化日期时间为特定的字符串:
const date = new Date();
console.log(date.toString());     // 转换为字符串表示
console.log(date.toISOString());  // 转换为 ISO 8601 格式的字符串
console.log(date.toLocaleString());  // 转换为本地日期时间字符串
  1. 对日期时间进行计算和操作:
const date = new Date();
date.setFullYear(2024);     // 设置年份
date.setMonth(2);           // 设置月份(0-11,2 表示三月)
date.setDate(15);           // 设置日期(1-31)
date.setHours(10);          // 设置小时(0-23)
date.setMinutes(30);        // 设置分钟(0-59)
date.setSeconds(45);        // 设置秒数(0-59)
date.setMilliseconds(500);  // 设置毫秒数
console.log(date);

这些只是Date对象的一些常见用法示例,您可以根据需要进一步探索Date对象的功能和方法。


Comments

Make a comment