1. Date()方法欢迎!我白天是个邮递员,晚上就是个有抱负的演员。这是我的网站。我住在天朝的帝都,有条叫做Jack的狗。
Date() 函数用于创建一个包含当前日期和时间的新 Date 对象。
const now = new Date();console.log(now);
输出:
Wed Sep 15 2021 15:21:45 GMT+0800 (中国标准时间)2. getTime()方法
getTime() 函数返回从 1970 年 1 月 1 日到现在的毫秒数。
const now = new Date();const timestamp = now.getTime();console.log(timestamp);
输出:
16316928964153. getFullYear()方法
getFullYear() 函数返回当前年份。
const now = new Date();const year = now.getFullYear();console.log(year);
输出:
20214. getDate()方法
getDate() 函数返回当前月份的日数。
const now = new Date();const day = now.getDate();console.log(day);
输出:15
5. getMonth()方法getMonth() 函数返回当前月份的数字(0-11)。
const now = new Date();const month = now.getMonth();console.log(month);
输出:
86. getDay()方法
getDay() 函数返回当前星期的数字(0-6)。
const now = new Date();const weekday = now.getDay();console.log(weekday);
输出:
37. getHours()方法
getHours() 函数返回当前时间的小时数。
const now = new Date();const hours = now.getHours();console.log(hours);
输出:15
8. getMinutes()方法getMinutes() 函数返回当前时间的分钟数。
const now = new Date();const minutes = now.getMinutes();console.log(minutes);
输出:31
9. getSeconds()方法getSeconds() 函数返回当前时间的秒数。
const now = new Date();const seconds = now.getSeconds();console.log(seconds);
输出:13
10. getMilliseconds()方法getMilliseconds() 函数返回当前时间的毫秒数。
const now = new Date();const milliseconds = now.getMilliseconds();console.log(milliseconds);
输出:
974
