日期对象Date

Date 对象也是对象,它也是像普通对象一样的用法,单独拿出来是因为它有很多自带的表示时间的方法。

Date 对象方法

方法 描述
Date() 返回当日的日期和时间。
getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。
getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。
getMonth() 从 Date 对象返回月份 (0 ~ 11)。
getFullYear() 从 Date 对象以四位数字返回年份。
getYear() 请使用 getFullYear() 方法代替。
getHours() 返回 Date 对象的小时 (0 ~ 23)。
getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。
getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。
getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。
getTime() 返回 1970 年 1 月 1 日至今的毫秒数。
setDate() 设置 Date 对象中月的某一天 (1 ~ 31)。
setMonth() 设置 Date 对象中月份 (0 ~ 11)。
setFullYear() 设置 Date 对象中的年份(四位数字)。
setYear() 请使用 setFullYear() 方法代替。
setHours() 设置 Date 对象中的小时 (0 ~ 23)。
setMinutes() 设置 Date 对象中的分钟 (0 ~ 59)。
setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)。
setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)。
setTime() 以毫秒设置 Date 对象。
toString() 把 Date 对象转换为字符串。
toTimeString() 把 Date 对象的时间部分转换为字符串。
toDateString() 把 Date 对象的日期部分转换为字符串。
valueOf() 返回 Date 对象的原始值。
var date = new Date();//它不是实时的,它是记录时刻的  console.log(date);
//Thu Mar 28 2019 11:22:19 GMT+0800 (中国标准时间) 
 过了一段时间再看date还是以前那个时刻的时间 
console.log(date)
//Thu Mar 28 2019 11:22:19 GMT+0800 (中国标准时间)

date.getMonth()//2
(返回月份,它是从0开始的,所以3月会返回2,一般表示月份都要date.getMonth + 1)
date.getDate();//28
(返回一个月中的某一天 (1 ~ 31)
date.getDay();//4
(星期四,但是实际上它表示的是一周中的某一天 (0 ~ 6),
也可以认为就是输出的星期几,但是星期天输出的为0),如下图:
0 1 2 3 4 5 6
星期天 星期一 星期二 星期三 星期四 星期五 星期六
date.getFullYear();//千年虫的解决办法:返回四位数字的年份
 date.getTime();//获取时间戳
(返回自197011日距离今日的一个毫秒数)
计算机的纪元时间为1970.01.01.00.00.00

猜你喜欢

转载自blog.csdn.net/weixin_43623871/article/details/88865641