时间标准
GMT
GMT 即 Greenwich Mean Time, 代表格林威治标准时间。对全球而言,这里所设定的时间是世界时间参考点,全球都以格林威治的时间作为标准来设定时间。
理论上来说,格林尼治标准时间的正午是指当太阳横穿格林尼治子午线时(也就是在格林尼治上空最高点时)的时间。由于地球在它的椭圆轨道里的运动速度不均匀,这个时刻可能与实际的太阳时有误差,最大误差达16分钟。
由于地球每天的自转是有些不规则的,而且正在缓慢减速,因此格林尼治时间已经不再被作为标准时间使用。现在的标准时间,是由原子钟报时的协调世界时(UTC)。
UTC
UTC 即 Coordinated Universal Time,代表世界协调时间或协调世界时。
UTC是经过平均太阳时(以格林威治时间GMT为准)、地轴运动修正后的新时标以及以“秒”为单位的国际原子时所综合精算而成的时间,计算过程相当严谨精密。
一般来说,当我们提到 UTC 时间 而不带任何别的修饰时,常指 UTC 0
点。
UTC 和 GMT 唯一的差别,UTC 有闰秒,GMT 没有。
CST
我们一般会以为它表示北京时间,不过CST这个缩写比较纠结的是它可以同时代表四个不同的时间:
- 美国中部时间:Central Standard Time (USA) UT-6:00
- 澳大利亚中部时间:Central Standard Time (Australia) UT+9:30
- 中国标准时间:China Standard Time UT+8:00
- 古巴标准时间:Cuba Standard Time UT-4:00
Date
创建date
类型 | 参数 |
---|---|
new Date() | 无参数 |
new Date(timestamp) | 时间戳 |
new Date(date string) | 日期字符串 |
new Date(year, month, day, hours, minutes, seconds, milliseconds) | 日期参数 |
-
无参数
如果没有提供参数,那么新创建的
Date
对象表示实例化时刻的日期和时间。new Date() // Mon Jun 14 2021 15:15:33 GMT+0800 (中国标准时间)
-
时间戳
在 JavaScript 中,时间戳是一个整数值,表示自
1970年1月1日00:00:00 UTC
(the Unix epoch)以来的毫秒数,忽略了闰秒。new Date(1623600000000) // Mon Jun 14 2021 00:00:00 GMT+0800 (中国标准时间) console.log(Date.now()) // 1623742921783
-
日期字符串
给
new Date
传递一个日期字符串来创建:let date = new Date('2021-06-14') console.log(date) // Mon Jun 14 2021 08:00:00 GMT+0800 (中国标准时间) let dateTime = new Date('2021-6-14 00:00:00') console.log(dateTime) // Mon Jun 14 2021 00:00:00 GMT+0800 (中国标准时间)
可以发现,直接输入
Date
和输入Date
+Time
得到的结果整整差了8个小时。我们来看一下 MDN 的解释:
注意: 由于浏览器之间的差异与不一致性,强烈不推荐使用
Date
构造函数来解析日期字符串 (或使用与其等价的Date.parse
)。对 RFC 2822 格式的日期仅有约定俗称的支持。 对 ISO 8601 格式的支持中,仅有日期的串 (例如 "1970-01-01") 会被处理为 UTC 而不是本地时间,与其他格式的串的处理不同。所以上面的代码例子中
date
实例化时,内部先获取倒了一个Mon Jun 14 2021 00:00:00 GMT+0000
这样的 UTC 时间,再被转为本地时区,就多了8个小时。dateTime
实例化时,Mon Jun 14 2021 00:00:00
被当做GMT+8000
的时区,所以得到的时间,就是0点。
需要补充的一点是:在小于GMT(0时区)的地方,直接使用
new Date(2021-6-14)
这样的方式实例化并且获取日期,永远会少一天。 -
分别提供日期与时间的每一个成员
你做多可以传递7个参数来创建日期:
year
:表示年份的整数值。monthIndex
:表示月份的整数值(0-11),默认值为0。date
可选 :表示一个月中的第几天的整数值(1-31),默认值为1。hours
可选 :表示一天中的小时数的整数值(24小时制)。默认值为0(午夜)。minutes
可选 :表示一个完整时间(如01:10:00
)中的分钟部分的整数值。默认值为0。seconds
可选 :表示一个完整时间(如01:10:00
)中的秒部分的整数值。默认值为0。millseconds
可选 :表示一个完整时间的毫秒部分的整数值。默认值为0。
let dateTime1 = new Date(2021,5,14,17,20,00) console.log(dateTime1) // Mon Jun 14 2021 17:20:00 GMT+0800 (中国标准时间) let dateTime2 = new Date(2021,13,14,17,20,00) console.log(dateTime2) // Mon Feb 14 2022 17:20:00 GMT+0800 (中国标准时间)
注意 参数
monthIndex
是从“0”开始计算的,这就意味着一月份为“0”,十二月份为“11”。注意: 当 Date 作为构造函数调用并传入多个参数时,如果数值大于合理范围时(如月份为 13 或者分钟数为 70),相邻的数值会被调整。比如
new Date(2013, 13, 1)
等于new Date(2014, 1, 1)
,它们都表示日期2014-02-01
(注意月份是从0开始的)。其他数值也是类似,new Date(2013, 2, 1, 0, 70)
等于new Date(2013, 2, 1, 1, 10)
,都表示同一个时间:2013-03-01T01:10:00
。注意:当 Date 作为构造函数调用并传入多个参数时,所定义参数代表的是当地时间。如果需要使用世界协调时 UTC,使用
new Date(Date.UTC(...))
和相同参数。
方法
Date.parse()
Date.parse()
方法接收一个表示日期的字符串参数,尝试将这个字符串转换为表示该日期的毫秒数。
let d1 = new Date('6/14/2021')
console.log(d1) // Mon Jun 14 2021 00:00:00 GMT+0800 (中国标准时间) 仅日期,UTC时区解析
let d2 = new Date('Jun 14 ,2021')
console.log(d2) // Mon Jun 14 2021 00:00:00 GMT+0800 (中国标准时间) 仅日期,UTC时区解析
let d3 = new Date('Mon Jun 14 2021 00:00:00 GMT+0800')
console.log(d3) // Mon Jun 14 2021 00:00:00 GMT+0800 (中国标准时间) 本地时区处理
let d4 = new Date('2021-6-14T00:00:00')
console.log(d4) // Invalid Date
Date.UTC()
UTC
方法接受以逗号隔开的日期参数,返回1970-1-1 00:00:00 UTC
到指定的日期之间的毫秒数。
UTC
方法与 Date
有两点不同:
Date.UTC
方法使用协调世界时代替本地时间。Date.UTC
方法返回一个时间数值,而不是一个日期对象。
let d5 = new Date(Date.UTC(2021, 5))
console.log(d5) // Tue Jun 01 2021 08:00:00 GMT+0800 (中国标准时间) 打印的本地时间,所以需要加8
console.log(d5.toUTCString()) // Tue, 01 Jun 2021 00:00:00 GMT 转换成UTC
let d6 = new Date(2021, 5)
console.log(d6) // Tue Jun 01 2021 00:00:00 GMT+0800 (中国标准时间)
let d7 = new Date(Date.UTC(2021, 5, 14, 20, 33, 33))
console.log(d7) // Tue Jun 15 2021 04:33:33 GMT+0800 (中国标准时间) 打印的本地时间,所以需要加8
console.log(d7.toUTCString()) // Mon, 14 Jun 2021 20:33:33 GMT 转换成UTC
let d8 = new Date(2021, 5, 14, 20, 33, 33)
console.log(d8) // Mon Jun 14 2021 20:33:33 GMT+0800 (中国标准时间)
格式化日期
const date = new Date(2021, 5, 14, 21, 59, 0)
console.log(date.toString()) // Mon Jun 14 2021 21:59:00 GMT+0800 (中国标准时间)
console.log(date.toLocaleString()) // 2021/6/14下午9:59:00
console.log(date.toDateString()) // Mon Jun 14 2021
console.log(date.toLocaleDateString()) // 2021/6/14
console.log(date.toTimeString()) // 21:59:00 GMT+0800 (中国标准时间)
console.log(date.toLocaleTimeString()) // 下午9:59:00
console.log(date.toUTCString()) // Mon, 14 Jun 2021 13:59:00 GMT
console.log(date.toISOString()) // 2021-06-14T13:59:00.000Z
操作毫秒数
const date = new Date(2021, 5, 15, 9, 56, 0)
console.log(date.valueOf()) // 1623722160000 该方法通常在 JavaScript 内部被调用,而不是在代码中显式调用。
console.log(date.getTime()) // 1623722160000 等同 valueOf 方法 返回从1970-1-1 00:00:00 UTC(协调世界时)到该日期经过的毫秒数,对于1970-1-1 00:00:00 UTC之前的时间返回负值。
console.log(Date.parse(date)) // 1623722160000
console.log(Date.UTC(2021, 5, 15, 9, 56, 0)) // 1623750960000
console.log(Date.now()) // 1623725066424 获取当前时间毫秒数
date.setMilliseconds(500) // 根据本地时间设置一个日期对象的豪秒数
console.log(date.getMilliseconds()) // 500 根据本地时间返回指定日期对象的毫秒
获取时区
getTimezoneOffset()
方法返回协调世界时(UTC)相对于当前时区的时间差值,单位为分钟。
const date = new Date(2021, 5, 15, 9, 56, 0)
console.log(date.getTimezoneOffset() / 60) // 如果本地时区早于协调世界时,则该差值为正值,如果晚于协调世界时则为负值。
操作年月日星期
//年
const date = new Date(2021, 5, 15, 9, 56, 0)
console.log(date.getFullYear()) // 2021
console.log(date.getUTCFullYear()) // 2021
date.setFullYear(0)
console.log(date.getFullYear()) // 0
date.setUTCFullYear(2021)
console.log(date.getUTCFullYear()) // 2021
console.log(date) // Tue Jun 15 2021 09:50:17 GMT+0800 (中国标准时间) 本地时间
console.log(date.toUTCString()) // Tue, 15 Jun 2021 01:50:17 GMT UTC时间
//月
const date = new Date(2021, 5, 15, 9, 56, 0)
console.log(date.getMonth()) // 5 六月
console.log(date.getUTCMonth()) // 5 六月
date.setMonth(1)
console.log(date.getMonth()) // 1 二月
date.setUTCMonth(10)
console.log(date.toUTCString()) // Mon, 15 Nov 2021 01:56:00 GMT
date.setMonth(15)
console.log(date.getMonth()) // 3 四月
date.setUTCMonth(21)
console.log(date.toUTCString()) // Sun, 15 Oct 2023 01:56:00 GMT
console.log(date.getUTCMonth(21)) // 9 十月
//日
const date = new Date(2021, 5, 15, 9, 56, 0)
console.log(date.getDate()) // 15
console.log(date.getUTCDate()) // 15
date.setDate(30)
console.log(date.getDate()) // 30
date.setUTCDate(15)
console.log(date.getUTCDate()) // 15
date.setDate(35)
console.log(date.getDate()) // 5 , 即 7 月 6 日(六月 30 + 5)
const date2 = new Date(2021, 5, 15, 9, 56, 0)
date2.setUTCDate(40)
console.log(date2.getUTCDate()) // 10 , 即 7 月 10 日 (六月 30 + 10)
//星期
const date = new Date(2021, 5, 15, 9, 56, 0)
console.log(date.getDay()) // 2 周二
date.setDate(55)
console.log(date) // Sun Jul 25 2021 09:56:00 GMT+0800 (中国标准时间)
console.log(date.getDay()) // 0 周日
const date2 = new Date(2021, 5, 15, 9, 56, 0)
date2.setUTCDate(55)
console.log(date2) // Sun Jul 25 2021 09:56:00 GMT+0800 (中国标准时间)
console.log(date2.getUTCDay()) // 0 周日
操作时分秒毫秒
//时
const date = new Date(2021, 5, 15, 9, 56, 0)
console.log(date) // Tue Jun 15 2021 09:56:00 GMT+0800 (中国标准时间) 本地时间
console.log(date.getHours()) // 9 本地时间
console.log(date.getUTCHours()) // 1 UTC时间
date.setHours(25)
console.log(date) // Wed Jun 16 2021 01:56:00 GMT+0800 (中国标准时间) 本地时间
date.setUTCHours(25)
console.log(date.toUTCString()) // Wed, 16 Jun 2021 01:56:00 GMT UTC时间
console.log(date.getUTCHours()) // 1 UTC时间
//分
const date = new Date(2021, 5, 15, 9, 56, 0)
console.log(date.getMinutes()) // 56
console.log(date.getUTCMinutes()) // 56
date.setMinutes(66)
console.log(date) // Tue Jun 15 2021 10:06:00 GMT+0800 (中国标准时间) 本地时间
console.log(date.getMinutes()) // 6
const date2 = new Date(2021, 5, 15, 9, 56, 0)
date2.setUTCMinutes(68)
console.log(date2.toUTCString()) // Tue, 15 Jun 2021 02:08:00 GMT
console.log(date2.getUTCMinutes()) // 8
//秒
const date = new Date(2021, 5, 15, 9, 56, 0)
console.log(date.getSeconds()) // 0
console.log(date.getUTCSeconds()) // 0
date.setSeconds(70)
console.log(date) // Tue Jun 15 2021 09:57:10 GMT+0800 (中国标准时间) 本地时间
console.log(date.getSeconds()) // 10
const date2 = new Date(2021, 5, 15, 9, 56, 0)
date2.setUTCSeconds(70)
console.log(date2.toUTCString()) // Tue, 15 Jun 2021 01:57:10 GMT UTC时间
console.log(date2.getUTCSeconds()) // 10
//毫秒
const date = new Date(2021, 5, 15, 9, 56, 0)
console.log(date.getMilliseconds()) // 0
console.log(date.getUTCMilliseconds()) // 0
date.setMilliseconds(700)
console.log(date.getMilliseconds()) // 700
date.setUTCMilliseconds(800)
console.log(date.getUTCMilliseconds()) // 800