一、Date()对象
let date = new Date();
//Sat Mar 02 2019 09:38:43 GMT+0800 (中国标准时间)
二、获取和设置改变具体:年月日时分秒
格林尼治时间=北京时间-8小时。
//现在时间2022-01-13
var d = new Date()
console.log(d.getDate()) //13
console.log(d.setDate(2), d)
//1641114724301 Sun Jan 02 2022 17:12:04 GMT+0800 (中国标准时间)
//***********************获取时间
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 日至今的毫秒数。 格林尼治时间至今的毫秒数
getTimezoneOffset() 返回本地时间与格林威治标准时间 (GMT) 的分钟差
//********************设置时间
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 对象。
setUTCDate() //根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。
setUTCMonth() //根据世界时设置 Date 对象中的月份 (0 ~ 11)。
setUTCFullYear() //根据世界时设置 Date 对象中的年份(四位数字)。
setUTCHours() //根据世界时设置 Date 对象中的小时 (0 ~ 23)。
setUTCMinutes() //根据世界时设置 Date 对象中的分钟 (0 ~ 59)。
setUTCSeconds() //根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。
setUTCMilliseconds() //根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。
三、获取时间戳
//1970年1月1日凌晨8点到现在的时间(中国在东八区,所以从8点开始算)
console.log(new Date().getTime()); //1551490841874
//前面添加加号直接转换
let date = +new Date(); //1551490841874
四、设置具体时间
new Date(时间格式) ====> 返回的都是北京标准时间
//参数为多个整数包括:年 月 日 时 分 秒 毫秒 注意:这里的月份是从0开始的
//注意: 字符串参数是时间节点 数字参数会默认为毫秒值
console.log(new Date('2020-10-1'))
console.log(new Date('2020/10/1'))
console.log(new Date('October 1,2020'))
console.log(new Date(2020, 9, 1, 0, 0, 0, 0))
// Thu Oct 01 2020 00:00:00 GMT+0800 (中国标准时间)
//这个参数是一个毫秒值 从1970年1月1日00:00:00开始加上这个一个毫秒值
console.log(new Date(1601481600000))
// Thu Oct 01 2020 00:00:00 GMT+0800 (中国标准时间)
五、其它方法
// 设置当前时间为
var timestamp3 = 1601481600000 //2020-10-1
var newDate = new Date() //中国标准时间
newDate.setTime(timestamp3) //日期对象设置时间
console.log(newDate) // Thu Oct 01 2020 00:00:00 GMT+0800 (中国标准时间)
console.log(console.log(newDate.toDateString())) //Thu Oct 01 2020
console.log(newDate.toGMTString()) //Wed, 30 Sep 2020 16:00:00 GMT
console.log(newDate.toISOString()) //2020-09-30T16:00:00.000Z
console.log(newDate.toJSON()) //2020-09-30T16:00:00.000Z
console.log(newDate.toLocaleDateString()) //2020/10/1
console.log(newDate.toLocaleString()) //2020/10/1 00:00:00
console.log(newDate.toLocaleTimeString()) //00:00:00
console.log(newDate.toString()) //thu Oct 01 2020 00:00:00 GMT+0800 (中国标准时间)
console.log(newDate.toTimeString()) //00:00:00 GMT+0800 (中国标准时间)
console.log(newDate.toUTCString()) // Wed, 30 Sep 2020 16:00:00 GMT
六、获取 某月有多少天
getMonthDay(year, month) {
let days = new Date(year, month, 0).getDate()
return days
},
链接: https://www.cnblogs.com/liuxuhui/p/12157226.html.
链接: https://www.jianshu.com/p/88d77c8ffe06.
链接: https://blog.csdn.net/weixin_44691513/article/details/107322910.
链接: https://zhuanlan.zhihu.com/p/162910462.