// 获取当前时间
let now = new Date();
console.log(now); // 输出当前日期和时间
// 获取年份
let year = now.getFullYear();
console.log(year); // 输出当前年份
// 获取月份(注意:月份是从0开始的,0表示1月)
let month = now.getMonth() + 1;
console.log(month); // 输出当前月份
// 获取日期
let date = now.getDate();
console.log(date); // 输出当前日期
// 获取小时
let hours = now.getHours();
console.log(hours); // 输出当前小时
// 获取分钟
let minutes = now.getMinutes();
console.log(minutes); // 输出当前分钟
// 获取秒数
let seconds = now.getSeconds();
console.log(seconds); // 输出当前秒数
// 获取星期几(0表示周日,1表示周一,以此类推)
let dayOfWeek = now.getDay();
console.log(dayOfWeek); // 输出当前是星期几
// 获取时间戳(从1970年1月1日到现在的毫秒数)
let timestamp = now.getTime();
console.log(timestamp); // 输出当前时间戳
new Date()
创建一个新的 Date
对象,表示当前日期和时间。getFullYear()
获取四位数的年份。getMonth()
获取月份,返回值为0到11,因此需要加1来得到实际的月份。getDate()
获取一个月中的某一天(1-31)。getHours()
获取小时(0-23)。getMinutes()
获取分钟(0-59)。getSeconds()
获取秒数(0-59)。getDay()
获取星期几(0-6,0表示周日)。getTime()
获取自1970年1月1日以来的毫秒数,即时间戳。上一篇:js 获取对象属性个数
下一篇:js 获取前一天
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站