Javascript中Date相关的知识总结

目录

获取时间

获取当前时间的时间戳的两种方式

获取年月日时分秒

Date()构造方法的参数说明

获取时间的方法

示例

注意几个易混淆易出错的方法

时间转换

Date中的T和Z说明,以及标准时间和北京时间的转换问题


获取时间

获取当前时间的时间戳的两种方式

1、 Date.now()

获取从 1970 年 1 月 1 日 0 时 0 分 0 秒(UTC,即协调世界时)到当前时间的毫秒数

2、new Date().getTime()

获取 从 1970 年 1 月 1 日 0 时 0 分 0 秒(UTC,即协调世界时)距离 Date 对象所代表时间的毫秒数。(更早的时间会用负数表示)

         //获取当前时间-时间戳
         const currentTime1 = Date.now()
         console.log('currentTime1: ', currentTime1)
         const currentTime12 = new Date().getTime()
         console.log('currentTime12: ', currentTime12)

获取年月日时分秒

Date()构造方法的参数说明

首先需要知道Date构造方法参数有以下几种情况

1、Date()

2、Date(时间字符串)

3、Date(时间戳)

4、Date(年,月,日,时,分,秒)

获取时间的方法

1、获取年getFullYear()

2、获取月

3、获取一个月中的第几天

4、获取一周中的第几天

5、获取时

6、获取分

7、获取秒

示例

         //  Date()不传递参数
         const date0 = new Date()
         const year0 = date0.getFullYear()
         const month0 = date0.getMonth() + 1
         const day0 = date0.getDate()
         const week0 = date0.getDay()
         const hour0 = date0.getHours();
         const minute0 = date0.getMinutes();
         const second0 = date0.getSeconds();
         console.log('year: ', year0, ' month: ', month0, ' day: ', day0, ' week: ', week0, ' hour: ', hour0,
             ' minute: ',
             minute0, ' second: ', second0)

         // Date传递时间字符串
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值