new Date 时间的常用方法,点赞收藏,很多你不知道
- getDate():获取日期中的日,返回值为1-31的整数。
const date = new Date();
console.log(date.getDate()); // 23
- getDay():获取日期中的星期几,返回值为0-6的整数,0表示星期日,1表示星期一,以此类推。
const date = new Date();
console.log(date.getDay()); // 2
- getFullYear():获取日期中的年份,返回值为4位数的整数。
const date = new Date();
console.log(date.getFullYear()); // 2021
- getHours():获取日期中的小时数,返回值为0-23的整数。
const date = new Date();
console.log(date.getHours()); // 10
- getMilliseconds():获取日期中的毫秒数,返回值为0-999的整数。
const date = new Date();
console.log(date.getMilliseconds()); // 123
- getMinutes():获取日期中的分钟数,返回值为0-59的整数。
const date = new Date();
console.log(date.getMinutes()); // 30
- getMonth():获取日期中的月份,返回值为0-11的整数,0表示一月,1表示二月,以此类推。
const date = new Date();
console.log(date.getMonth()); // 8
- getSeconds():获取日期中的秒数,返回值为0-59的整数。
const date = new Date();
console.log(date.getSeconds()); // 45
- getTime():获取日期的时间戳,返回值为从1970年1月1日00:00:00 UTC到当前日期的毫秒数。
const date = new Date();
console.log(date.getTime()); // 1632384645123
- getTimezoneOffset():获取当前时区与UTC时间的分钟差,返回值为负数表示当前时区比UTC时间早,正数表示当前时区比UTC时间晚。
const date = new Date();
console.log(date.getTimezoneOffset()); // -480
- setDate():设置日期中的日,参数为1-31的整数。
const date = new Date();
date.setDate(25);
console.log(date.getDate()); // 25
- setFullYear():设置日期中的年份,参数为4位数的整数。
const date = new Date();
date.setFullYear(2022);
console.log(date.getFullYear()); // 2022
- setHours():设置日期中的小时数,参数为0-23的整数。
const date = new Date();
date.setHours(12);
console.log(date.getHours()); // 12
- setMilliseconds():设置日期中的毫秒数,参数为0-999的整数。
const date = new Date();
date.setMilliseconds(500);
console.log(date.getMilliseconds()); // 500
- setMinutes():设置日期中的分钟数,参数为0-59的整数。
const date = new Date();
date.setMinutes(45);
console.log(date.getMinutes()); // 45
- setMonth():设置日期中的月份,参数为0-11的整数,0表示一月,1表示二月,以此类推。
const date = new Date();
date.setMonth(11);
console.log(date.getMonth()); // 11
- setSeconds():设置日期中的秒数,参数为0-59的整数。
const date = new Date();
date.setSeconds(30);
console.log(date.getSeconds()); // 30
- setTime():设置日期的时间戳,参数为从1970年1月1日00:00:00 UTC到当前日期的毫秒数。
const date = new Date();
date.setTime(1632384645123);
console.log(date.getTime()); // 1632384645123
-
toDateString() - 将当前日期转换为字符串,格式为"星期 月 日 年"
例:new Date().toDateString() // 返回类似"Wed Jul 20 2022"的字符串
-
toISOString() - 将当前日期转换为ISO格式的字符串,格式为"yyyy-MM-ddTHH:mm:ss.sssZ"
例:new Date().toISOString() // 返回类似"2022-07-20T06:30:45.123Z"的字符串
-
toJSON() - 将当前日期转换为JSON格式的字符串,格式为"yyyy-MM-ddTHH:mm:ss.sssZ"
例:new Date().toJSON() // 返回类似"2022-07-20T06:30:45.123Z"的字符串
-
toLocaleDateString() - 将当前日期转换为本地日期格式的字符串
例:new Date().toLocaleDateString() // 返回类似"2022/7/20"的字符串
-
toLocaleString() - 将当前日期转换为本地日期时间格式的字符串
例:new Date().toLocaleString() // 返回类似"2022/7/20 下午2:30:45"的字符串
-
toLocaleTimeString() - 将当前时间转换为本地时间格式的字符串
例:new Date().toLocaleTimeString() // 返回类似"下午2:30:45"的字符串
-
toString() - 将当前日期转换为字符串,格式为"星期 月 日 年 HH:mm:ss 时区"
例:new Date().toString() // 返回类似"Wed Jul 20 2022 14:30:45 GMT+0800 (中国标准时间)"的字符串
-
toTimeString() - 将当前时间转换为字符串,格式为"HH:mm:ss 时区"
例:new Date().toTimeString() // 返回类似"14:30:45 GMT+0800 (中国标准时间)"的字符串
-
toUTCString() - 将当前日期转换为UTC时间格式的字符串
例:new Date().toUTCString() // 返回类似"Wed, 20 Jul 2022 06:30:45 GMT"的字符串
-
UTC() - 返回指定UTC时间的毫秒数
例:Date.UTC(2022, 6, 20, 14, 30, 45) // 返回指定时间的毫秒数,比如 1655759445000
-
valueOf() - 返回当前日期的毫秒数
例:new Date().valueOf() // 返回当前日期的毫秒数,比如 1647460130123
-
now() - 返回当前时间的毫秒数
例:Date.now() // 返回当前时间的毫秒数,比如 1647460130123
还有.....