Javascript中Date相关的知识总结

本文总结了JavaScript中Date对象的使用,包括获取当前时间的时间戳、年月日时分秒的方法,以及Date构造方法的参数说明。还讨论了时间转换中的T和Z的含义,以及标准时间和北京时间的转换问题,解析了在不同时间点转换时可能出现的特殊现象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

获取时间

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

获取年月日时分秒

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传递时间字符串--年月日的形式
         const date = new Date('2022-09-7 22:09:09')
         const year = date.getFullYear()
         const month = date.getMonth() + 1
         const day = date.getDate()
         const week = date.getDay()
         const hour = date.getHours();
         const minute = date.getMinutes();
         const second = date.getSeconds();
         console.log('year: ', year, ' month: ', month, ' day: ', day, ' week: ', week, ' hour: ', hour, ' minute: ',
             minute, ' second: ', second)

         // Date传递时间字符串--时间戳的形式
         const date2 = new Date(1662561716215)
         const year2 = date2.getFullYear()
         const month2 = date2.getMonth() + 1
         const day2 = date2.getDate()
         const week2 = date2.getDay()
         const hour2 = date2.getHours();
         const minute2 = date2.getMinutes();
         const second2 = date2.getSeconds();
         console.log('year: ', year2, ' month: ', month2, ' day: ', day2, ' week: ', week2, ' hour: ', hour2,
             ' minute: ', minute2, ' second: ', second2)

         // Date传递时间字符串--月日年时分秒的形式
         const date3 = new Date('September 7, 2022 11:45:12');
         const year3 = date3.getFullYear()
         const month3 = date3.getMonth() + 1
         const day3 = date3.getDate()
         const week3 = date3.getDay()
         const hour3 = date3.getHours();
         const minute3 = date3.getMinutes();
         const second3 = date3.getSeconds();
         console.log('year: ', year3, ' month: ', month3, ' day: ', day3, ' week: ', week3, ' hour: ', hour3,
             ' minute: ', minute3, ' second: ', second3)

         // Date传递年,月,日,时,分,秒的具体数值
         const date4 = new Date(2022, 8, 7, 22, 48, 13); //注意传递月份的时候需要实际月份减1
         const year4 = date4.getFullYear()
         const month4 = date4.getMonth() + 1
         const day4 = date4.getDate()
         const week4 = date4.getDay()
         const hour4 = date4.getHours();
         const minute4 = date4.getMinutes();
         const second4 = date4.getSeconds();
         console.log('year: ', year4, ' month: ', month4, ' day: ', day4, ' week: ', week4, ' hour: ', hour4,
             ' minute: ', minute4, ' second: ', second4)

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

1、getFullYear()和getYear()的区别

getFullYear()获取的是Date 对象的完整年份(四位数年份)

getYear()获取的是相对时间(相对 1900 年)

2、getDate()和getDay()的区别

getDate()获取的是一个月中的哪一天,其值的范围为1~31

getDay()获取的是一个星期中的哪一天,其值的范围为0~6,0表示星期天

3、为什么getMonth()需要加1

getMonth()返回的是Date 对象的月份,其值的范围是0~11,0 表示一年中的第一月,所以我们在获取实际月份时需要getMonth()+1

时间转换

         // 时间戳转换为年月日时分秒
         const date = new Date(1662561716215)
         const year = date.getFullYear()
         const month = date.getMonth() + 1
         const day = date.getDate()
         const hour = date.getHours();
         const minute = date.getMinutes();
         const second = date.getSeconds();
         const time =
             `${year}-${dealDate(month)}-${dealDate(day)} ${dealDate(hour)}:${dealDate(minute)}:${dealDate(second)}`
         console.log('time: ', time)

         function dealDate(value) {
             return value < 10 ? '0' + value : value
         }

 

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

T表示分隔符

Z表示时区

国际标准时间格式为:2022-09-07T23:00:00.000Z

北京时间格式:2022-09-07 00:00:00

因为北京时间是东八区,所以在标准时间上加上8小时就是东八区时间,也就是北京时间。

也因此在16点或者16点之后的标准时间转成北京时间时会多1

2022-09-07T15:59:59.000Z通过getDate()获取的值是7

2022-09-07T16:00:00.000Z通过getDate()获取的值是8

         const date = new Date('2022-09-07T15:59:59.000Z')
         const year = date.getFullYear()
         const month = date.getMonth() + 1
         const day = date.getDate()
         const week = date.getDay()
         const hour = date.getHours();
         const minute = date.getMinutes();
         const second = date.getSeconds();
         console.log('2022-09-07T15:59:59.000Z  year: ', year, ' month: ', month, ' day: ', day, ' week: ', week, ' hour: ', hour, ' minute: ',
             minute, ' second: ', second)

         const date2 = new Date('2022-09-07T16:00:00.000Z')
         const year2 = date2.getFullYear()
         const month2 = date2.getMonth() + 1
         const day2 = date2.getDate()
         const week2 = date2.getDay()
         const hour2 = date2.getHours();
         const minute2 = date2.getMinutes();
         const second2 = date2.getSeconds();
         console.log('2022-09-07T16:00:00.000Z  year: ', year2, ' month: ', month2, ' day: ', day2, ' week: ', week2, ' hour: ', hour2,
             ' minute: ', minute2, ' second: ', second2)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值