简介express
JavaScript经过强大的对象为咱们提供日期处理功能:日期。浏览器
本文确实_不是_谈论 Moment.js ,我认为它是处理日期的最佳库,你应该在处理日期时几乎老是使用它。
Date对象实例表示单个时间点。this
尽管被命名为Date,它也处理时间。spa
咱们使用初始化Date对象翻译
new Date()
这将建立一个指向当前时刻的Date对象。code
在内部,日期以1970年1月1日(UTC)以来的毫秒数表示。这个日期很重要,由于就计算机而言,这就是一切开始的地方。orm
您可能熟悉UNIX时间戳:它表示自该着名日期以来通过的seconds数。对象
重要:UNIX时间戳的缘由以秒为单位。JavaScript以毫秒为单位记录缘由。blog
若是咱们有UNIX时间戳,咱们能够使用实例化JavaScript Date对象ip
const timestamp = 1530826365 new Date(timestamp * 1000)
若是咱们传递0,咱们将获得一个Date对象,表示1970年1月1日(UTC)的时间:
new Date(0)
若是咱们传递一个字符串而不是一个数字,那么Date对象使用parse方法来肯定您传递的日期。例子:
new Date('2018-07-22') new Date('2018-07') //July 1st 2018, 00:00:00 new Date('2018') //Jan 1st 2018, 00:00:00 new Date('07/22/2018') new Date('2018/07/22') new Date('2018/7/22') new Date('July 22, 2018') new Date('July 22, 2018 07:22:13') new Date('2018-07-22 07:22:13') new Date('2018-07-22T07:22:13') new Date('25 March 2018') new Date('25 Mar 2018') new Date('25 March, 2018') new Date('March 25, 2018') new Date('March 25 2018') new Date('March 2018') //Mar 1st 2018, 00:00:00 new Date('2018 March') //Mar 1st 2018, 00:00:00 new Date('2018 MARCH') //Mar 1st 2018, 00:00:00 new Date('2018 march') //Mar 1st 2018, 00:00:00
这里有不少灵活性。您能够在几个月或几天内添加或省略前导零。
当心月/日的位置,或者你可能最终将月份误解为当天。
你也能够使用Date.parse:
Date.parse('2018-07-22') Date.parse('2018-07') //July 1st 2018, 00:00:00 Date.parse('2018') //Jan 1st 2018, 00:00:00 Date.parse('07/22/2018') Date.parse('2018/07/22') Date.parse('2018/7/22') Date.parse('July 22, 2018') Date.parse('July 22, 2018 07:22:13') Date.parse('2018-07-22 07:22:13') Date.parse('2018-07-22T07:22:13')
Date.parse将返回一个时间戳(以毫秒为单位)而不是Date对象。
您还能够传递一组表明日期各部分的有序值:年,月(从0开始),日,小时,分钟,秒和毫秒:
new Date(2018, 6, 22, 7, 22, 13, 0) new Date(2018, 6, 22)
最小值应该是3个参数,可是大多数JavaScript引擎的解释都比这些少:
new Date(2018, 6) //Sun Jul 01 2018 00:00:00 GMT+0200 (Central European Summer Time) new Date(2018) //Thu Jan 01 1970 01:00:02 GMT+0100 (Central European Standard Time)
在任何这些状况下,生成的日期都相对于计算机的时区。这意味着两台不一样的计算机可能会为同一日期对象输出不一样的值。
JavaScript没有任何关于时区的信息,会将日期视为UTC,并自动执行到当前计算机时区的转换。
所以,总结一下,您能够经过4种方式建立新的Date对象
初始化日期时,您能够传递时区,所以日期不会被假定为UTC,而后转换为您当地的时区。
您能够经过以+ HOURS格式添加时区来指定时区,或者经过添加括在括号中的时区名称来指定时区:
new Date('July 22, 2018 07:22:13 +0700') new Date('July 22, 2018 07:22:13 (CET)')
若是在括号中指定了错误的时区名称,则JavaScript将默认为UTC而不会报错。
若是您指定了错误的数字格式,JavaScript将报“无效日期”的错误。
给定Date对象,有不少方法将从该日期生成一个字符串:
const date = new Date('July 22, 2018 07:22:13') date.toString() // "Sun Jul 22 2018 07:22:13 GMT+0200 (Central European Summer Time)" date.toTimeString() //"07:22:13 GMT+0200 (Central European Summer Time)" date.toUTCString() //"Sun, 22 Jul 2018 05:22:13 GMT" date.toDateString() //"Sun Jul 22 2018" date.toISOString() //"2018-07-22T05:22:13.000Z" (ISO 8601 format) date.toLocaleString() //"22/07/2018, 07:22:13" date.toLocaleTimeString() //"07:22:13" date.getTime() //1532236933000 date.getTime() //1532236933000
Date对象提供了几种检查其值的方法。这些都取决于计算机的当前时区:
const date = new Date('July 22, 2018 07:22:13') date.getDate() //22 date.getDay() //0 (0 means sunday, 1 means monday..) date.getFullYear() //2018 date.getMonth() //6 (starts from 0) date.getHours() //7 date.getMinutes() //22 date.getSeconds() //13 date.getMilliseconds() //0 (not specified) date.getTime() //1532236933000 date.getTimezoneOffset() //-120 (will vary depending on where you are and when you check - this is CET during the summer). Returns the timezone difference expressed in minutes
这些方法有等效的UTC版本,它们返回UTC值而不是适合您当前时区的值:
date.getUTCDate() //22 date.getUTCDay() //0 (0 means sunday, 1 means monday..) date.getUTCFullYear() //2018 date.getUTCMonth() //6 (starts from 0) date.getUTCHours() //5 (not 7 like above) date.getUTCMinutes() //22 date.getUTCSeconds() //13 date.getUTCMilliseconds() //0 (not specified)
Date对象提供了几种编辑日期值的方法:
const date = new Date('July 22, 2018 07:22:13') date.setDate(newValue) date.setDay(newValue) date.setFullYear(newValue) //note: avoid setYear(), it's deprecated date.setMonth(newValue) date.setHours(newValue) date.setMinutes(newValue) date.setSeconds(newValue) date.setMilliseconds(newValue) date.setTime(newValue) date.setTimezoneOffset(newValue)
setDay和setMonth从0开始编号,所以例如March是2月。
你能够在setHours()中添加多个参数来设置分钟,秒和毫秒:setHours(0,0,0,0) - 这一样适用于setMinutes和setSeconds。
至于get_,set_方法也有UTC等价物:
const date = new Date('July 22, 2018 07:22:13') date.setUTCDate(newalue) date.setUTCDay(newValue) date.setUTCFullYear(newValue) date.setUTCMonth(newValue) date.setUTCHours(newValue) date.setUTCMinutes(newValue) date.setUTCSeconds(newValue) date.setUTCMilliseconds(newValue)
若是要以毫秒为单位获取当前时间戳,能够使用速记
Date.now()
代替
new Date().getTime()
请注意。若是您使用天数计算超过一个月,则不会出现错误,日期将转到下个月:
new Date(2018, 6, 40) //Thu Aug 09 2018 00:00:00 GMT+0200 (Central European Summer Time)
数月,小时,分钟,秒和毫秒都是如此。
现代浏览器中的支持良好国际化API(值得注意的例外:UC浏览器)容许您翻译日期。
它是由Intl Object 暴露出来的,这也有助于本地化数字,字符串。
我来看看Intl.DateTimeFormat()。
如下是如何使用它。
根据计算机默认区域设置格式化日期:
// "12/22/2017" const date = new Date('July 22, 2018 07:22:13') new Intl.DateTimeFormat().format(date) //"22/07/2018" in my locale
根据不一样的区域设置格式化日期:
new Intl.DateTimeFormat('en-US').format(date) //"7/22/2018" Intl.DateTimeFormat方法采用可选参数,容许您自定义输出显示小时,分钟和秒: const options = { year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' } new Intl.DateTimeFormat('en-US', options).format(date) //"7/22/2018, 7:22:13 AM" new Intl.DateTimeFormat('it-IT', options2).format(date) //"22/7/2018, 07:22:13"
您能够使用Date.getTime()计算两个日期之间的差别:
const date1 = new Date('July 10, 2018 07:22:13') const date2 = new Date('July 22, 2018 07:22:13') const diff = date2.getTime() - date1.getTime() //difference in milliseconds
以一样的方式,您能够检查两个日期是否相等:
const date1 = new Date('July 10, 2018 07:22:13') const date2 = new Date('July 10, 2018 07:22:13') if (date2.getTime() === date1.getTime()) { //dates are equal }
请记住,getTime()返回的毫秒数,所以您须要在比较中考虑时间因素。2018年7月10日07:22:13 不等于2018年7月10日。在这种状况下,您能够使用setHours(0,0,0,0)重置时间。