日期对象

日期

获取日期是一个很简单的操作,但我们需要将日期对象进行处理,处理成我们需要的格式,放在任何地方。
首先,简单介绍一下日期对象。
日期对象:获取的是本地电脑的时间(注意是一个对象,不是字符串)

console.log( new Date());
console.dir( new Date());//可以看出是一个对象
//结果如下:
//Fri May 08 2020 09:38:37 GMT+0800 (中国标准时间)

常用日期对象的使用:

let date = new Date();
//年月日时分秒(获取的都是数字)
console.log( date.getFullYear());//年
console.log( date.getMonth()+1);//月,从0开始,获取当前月要+1
console.log( date.getDate());//日
console.log( date.getHours());//时
console.log( date.getMinutes());//分
console.log( date.getSeconds());//秒

//星期(返回数字)
//0-6:周日-周六
console.log( date.getDay());//周几

//转换成国际标准时间字符串
console.log( date.toUTCString());

还可以设置时间:时间戳

 console.log( new Date(2020,4,8));
 //Fri May 08 2020 00:00:00 GMT+0800 (中国标准时间)
 console.log( new Date(2020,4,8,20,0,0));
 //Fri May 08 2020 20:00:00 GMT+0800 (中国标准时间)
 console.log( new Date(2020,1));
 //Sat Feb 01 2020 00:00:00 GMT+0800 (中国标准时间)
 
  //只有一个参数时,会认为是一个毫秒数
  console.log( new Date(2020));
  //Thu Jan 01 1970 08:00:02 GMT+0800 (中国标准时间)
  //默认时间Thu Jan 01 1970 08:00:00 GMT+0800 (中国标准时间)

日期对象
和 数字做减法——>得到相差的毫秒数
和 别的时间做减法——>得到相差的毫秒数

let date = new Date();
    console.log( date-0);
    //当前时间毫秒数:1588903751019
    
    let birthday = new Date(2000,3,20);
    console.log( date-birthday);
    //生日与现在相差的毫秒数:632743941011
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值