镇楼图

Pixiv:torino



Date

构造Date

JS中Date提供了时间日期相关的操作

Unix时间戳:指自1970 年 1 月 1 日 00:00:00 UTC开始经过的毫秒数,为一整数值,这也是大多数编程语言的实现

时间戳字符串:表示日期的字符串,必须存在年月,且可选是否要添加日、时、分、秒、毫秒

■构造Date对象

new Date();
new Date(value);
new Date(dateString);
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);

第一种方法不添加任何参数,即调用Date中的now方法,创建一个当前时间的Unix时间戳

第二种方法可添加Unix时间戳,但这个可能并没有那么直观

第三种方法可以添加时间戳字符串

第四种方法,会根据不太参数来创建Date

创建方法

Date.now()可以获取当前时间

Date.parse(datestring)可以解析时间戳字符串,字符串格式符合RFC2822或ISO 8601。若无法被正确解析,返回NaN

console.log(Date.parse("2022-12-30"));
Date.UTC(year)
Date.UTC(year, month)
Date.UTC(year, month, day)
Date.UTC(year, month, day, hour)
Date.UTC(year, month, day, hour, minute)
Date.UTC(year, month, day, hour, minute, second)
Date.UTC(year, month, day, hour, minute, second, millisecond)

博主认为UTC方法更常用,parse方法关乎字符串的格式,可能并不是那么好处理

输出值

Date.prototype.toJSON()方法是针对JSON序列化时的输出,对象可内置toJSON方法,当JSON.stringify所序列化的对象存在toJSON方法时会直接调用此方法。Date的toJSON方法可以返回ISOString的形式

console.log(JSON.stringify(new Date()));
//相当于调用toJSON方法

关于ISOString则是Date.prototype.toISOString()方法内的,将会返回ISO 8601格式的字符串即YYYY-MM-DDTHH:mm:ss.sssZ形式,可完整显示日期以及时间

console.log(new Date().toISOString());

Date.prototype.toString()将会输出本地时区的时间

Date.prototype.toDateString()将会输出日期部分

Date.prototype.toTimeString()将会输出时间部分且附带时区信息

console.log(new Date().toString());
console.log(new Date().toDateString());
console.log(new Date().toTimeString());

Date.prototype.toUTCString()则会将时区转成UTC后输出

console.log(new Date().toUTCString());

Date.prototype.valueOf()将会返回Unix时间戳

console.log(new Date().valueOf());

获取Date值

Date.prototype.getFullyear()可获取年份

Date.prototype.getMonth()可获取月份(0表示一月)

Date.prototype.getDate()可获取日期

Date.prototype.getDay()可获取星期(0表示周日)

Date.prototype.getHours()可获取小时

Date.prototype.getMinutes()可获取分钟

Date.prototype.getSeconds()可获取秒

Date.prototype.getMilliseconds()可获取毫秒

let days = ["星期天","星期一","星期二","星期三","星期四","星期五","星期六"]
let nowdate = new Date();
console.log("年份:"+nowdate.getFullYear());
console.log("月份:"+(nowdate.getMonth()+1));
console.log("日期:"+nowdate.getDate());
console.log("星期:"+days[nowdate.getDay()]);
console.log("时:"+nowdate.getHours());
console.log("分:"+nowdate.getMinutes());
console.log("秒:"+nowdate.getSeconds());
console.log("毫秒:"+nowdate.getMilliseconds());

Date.prototype.getTime()会获取相应的Unix时间戳

console.log(new Date().getTime());
//可在某段程序开头和执行后创建Date
//来比较时间戳获取运行时间(ms)

除了相应的时间外还有对UTC时间的处理

Date.prototype.getTimezoneOffset()会获取与UTC相差的时间(分钟)

console.log(new Date().getTimezoneOffset());
//处在中国则会返回-480min

Date.prototype.getUTCFullyear()可获取相应UTC时区的年份

Date.prototype.getUTCMonth()可获取相应UTC时区的月份(0表示一月)

Date.prototype.getUTCDate()可获取相应UTC时区的日期

Date.prototype.getUTCDay()可获取相应UTC时区的星期(0表示周日)

Date.prototype.getUTCHours()可获取相应UTC时区的小时

Date.prototype.getUTCMinutes()可获取相应UTC时区的分钟

Date.prototype.getUTCSeconds()可获取相应UTC时区的秒

Date.prototype.getUTCMilliseconds()可获取相应UTC时区的毫秒

设置Date值

Date.prototype.setFullYear(yearvalue [, monthvalue ] [, dayvalue ] )可设置年月日(月份同get方法一致,0为1月)若未指定monthvalue、dayvalue则会执行getMonth、getDate方法获取(即不改变月、日)

Date.prototype.setMonth(monthvalue [, dayvalue ] )可设置月日

Date.prototype.setDate(dayvalue)可设置日期

Date.prototype.setHours(hourvalue [, minutevalue ] [, svalue ] [, msvalue ] )可设置时分秒毫秒

Date.prototype.setMinutes(minutevalue [, svalue ] [, msvalue ] )可设置分秒毫秒

Date.prototype.setSeconds(svalue [, msvalue ] )可设置秒毫秒

Date.prototype.setMilliseconds( msvalue )可设置毫秒

let date = new Date();
date.setFullYear(2012,5,12);
date.setHours(7,15,5,666);
console.log(date.toString());

Date.prototype.setTime(value)可设置Unix时间戳(即设定了时间的年月日时分秒毫秒)

此外Date也有相关UTC设置方法,它会设置相应的UTC时间,但实际输出如果没按UTC输出则是另外的经过时区运算后的时间

Date.prototype.setUTCFullYear(yearvalue [, monthvalue ] [, dayvalue ] )可设置UTC时间的年月日

Date.prototype.setUTCMonth(monthvalue [, dayvalue ] )可设置UTC时间的月日

Date.prototype.setUTCDate(dayvalue)可设置UTC时间的日期

Date.prototype.setUTCHours(hourvalue [, minutevalue ] [, svalue ] [, msvalue ] )可设置UTC时间的时分秒毫秒

Date.prototype.setUTCMinutes(minutevalue [, svalue ] [, msvalue ] )可设置UTC时间的分秒毫秒

Date.prototype.setUTCSeconds(svalue [, msvalue ] )可设置UTC时间的秒毫秒

Date.prototype.setUTCMilliseconds( msvalue )可设置UTC时间的毫秒



参考资料

[1] 《JavaScrpit DOM 编程艺术》

[2] MDN

[3] 现代JS教程

[4] 黑马程序员 JS pink

posted on 2022-12-30 04:17  摸鱼鱼的尛善  阅读(53)  评论(0编辑  收藏  举报