// 创建一个新的 Date 对象,表示当前日期和时间
let now = new Date();
console.log(now); // 输出当前日期和时间
// 创建一个表示特定日期的 Date 对象
let birthday = new Date('1990-05-24');
console.log(birthday); // 输出 1990 年 5 月 24 日的日期和时间
// 获取当前年份、月份和日期
let year = now.getFullYear();
let month = now.getMonth() + 1; // getMonth() 返回的是 0-11 的值,所以要加 1
let day = now.getDate();
console.log(`${year}-${month}-${day}`); // 输出格式化的日期字符串
// 获取当前小时、分钟和秒
let hours = now.getHours();
let minutes = now.getMinutes();
let seconds = now.getSeconds();
console.log(`${hours}:${minutes}:${seconds}`); // 输出格式化的时间字符串
// 设置日期和时间
let futureDate = new Date();
futureDate.setFullYear(2025, 11, 25); // 设置为 2025 年 12 月 25 日
futureDate.setHours(23, 59, 59, 999); // 设置时间为 23:59:59.999
console.log(futureDate); // 输出设置后的日期和时间
new Date():创建一个表示当前日期和时间的 Date 对象。new Date('1990-05-24'):创建一个表示特定日期的 Date 对象。getFullYear()、getMonth()、getDate():分别获取年份、月份(从 0 开始)和日期。getHours()、getMinutes()、getSeconds():分别获取小时、分钟和秒。setFullYear()、setHours() 等方法用于设置日期和时间。上一篇:js 查看对象类型
下一篇:js 创建对象
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站