最新更新时间:2020年05月28日13:59:00
《猛戳-查看我的博客地图-总有你意想不到的惊喜》
本文内容:Date对象,用于处理日期和时间
创建Date对象
var date = new Date();
var date = new Date(milliseconds);
var date = new Date('Fri Mar 24 2017 16:42:54 GMT+0800 (中国标准时间)');
var date = new Date(year,month,day,hours,minutes,seconds,milliseconds);
Date对象实例的方法
var date = new Date();
console.log(date);//object类型的输出 > Fri Mar 24 2017 16:42:54 GMT+0800 (中国标准时间)
console.log(date.timeString());//16:46:19 GMT+0800 (中国标准时间)
console.log(date.String());//Fri Mar 24 2017 16:46:19 GMT+0800 (中国标准时间)
console.log(date.toLocaleString());//2017/3/24 下午4:46:19
console.log(date.toLocaleDateString());//2017/3/24
console.log(date.toLocaleTimeString());//下午4:46:19
console.log(date.toTimeString());//16:46:19 GMT+0800 (中国标准时间)
console.log(date.toJSON());//2017-03-24T08:46:19.944Z 注意:比中国当前时间少8个小时
console.log(date.toISOString());//2017-03-24T09:07:43.633Z
console.log(date.toDateString());//Fri Mar 24 2017
console.log(date.getDate());//一个月中的某一天
console.log(date.getDay());//一周中的某一天 0周日 1周一 ... 6周六
console.log(date.getFullYear());//四位数年份
console.log(date.getHours());//小时
console.log(date.getMilliseconds());//毫秒数
console.log(date.getMinutes());//
console.log(date.getMonth());//月份 0-11
console.log(date.Seconds());//
console.log(date.valueOf());//返回1970.1.1至今的毫秒数 > 1490344974156
console.log(date.getTime());//返回1970.1.1至今的毫秒数
常用的格式
- 格式一:
2020-04-14 14:13:04
let date = new Date().toJSON().slice(0, 10) + ' ' + new Date().toTimeString().slice(0, 8);
- 格式二,时间戳:
1590645910584
let date = new Date().getTime();
- 格式三:
2020-04-14
let date = new Date().toJSON().slice(0, 10);
- 格式四:
14:13:04
let date = new Date().toTimeString().slice(0, 8);
- 格式五:
2020/5/28
let date = new Date().toLocaleDateString();
- 格式六,获取指定时间区间的时间戳
20200321
凌晨到20200324
晚上12点
new Date(2020,2,21,0,0,0,0).getTime()
new Date(2020,2,24,23,59,59,0).getTime()
注意:月份从0起算,1月份对应的是0
- 格式七,获取星期几
let date = new Date().getDay();//0 1 2 3 4 5 6
getDay | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
星期几 | 周日 | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 |
参考资料
- 无
感谢阅读,欢迎评论^-^
打赏我吧^-^