JS中使用Date对象来表示一个时间
使用构造函数直接创建一个Date对象,会封装代码执行时的时间
var d = new Date(); document.write(d); //Sat Apr 10 2021 10:57:55 GMT+0800 (中国标准时间)
使用构造函数创建一个指定的Date对象
var d2 = new Date("2021,3,22,11:30:45"); document.write(d2); //Mon Mar 22 2021 11:30:45 GMT+0800 (中国标准时间)
方法 | 描述 |
---|---|
getDate() | 从Date对象返回一月中的某一天(1~31) |
getDay | 从 Date 对象返回一周中的某一天 (0 ~ 6,0指周天) |
getFullyear | 从 Date 对象以四位数字返回年份 |
getMonth() | 从 Date 对象返回月份 (0 ~ 11) |
getTime() | 返回 1970 年 1 月 1 日至今的毫秒数 |
更多Date对象的方法:https://www.runoob.com/jsref/jsref-obj-date.html