JavaScript Date对象详解和实例

最新更新时间: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);
let date = new Date().toJSON().slice(0, 10).replace(/\-/g, '.');//2020.04.14
  • 格式四: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
getDay0123456
星期几周日周一周二周三周四周五周六
常用的方法
  • 毫秒数转时分秒,666666 -> 00:11:06
let eightHour = 8*60*60*1000;//8个小时的的毫秒数
let millisecond = 666666;//毫秒数
let date = new Date(millisecond-eightHour).toTimeString().slice(0, 8);// 时分秒 "00:11:06"
参考资料

感谢阅读,欢迎评论^-^

打赏我吧^-^

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值