Module: datetime

日期时间相关函数

Namespaces

Functions

addTime

addTime(date, amount, type?): Date

对日期时间进行量变处理

Example

//2020/5/1 08:00:20console.log(_.formatDate(_.addTime(new Date('2020-05-01'),20),'yyyy/MM/dd hh:mm:ss'))//2020-04-11 08:00console.log(_.formatDate(_.addTime(new Date('2020-05-01'),-20,'d')))//2022-01-01 00:00console.log(_.formatDate(_.addTime(new Date('2020-05-01 0:0'),20,'M')))

Parameters

NameTypeDescription
datestring | number | Date原日期时间
amountnumber变化量,可以为负数
type?string量变时间类型
  • y
  • M
  • d
  • h
  • m
  • s

Returns

Date

日期对象


compareDate

compareDate(date1, date2, type?): number

比较两个日期,并返回由比较时间单位确定的相差时间。

使用truncated对比算法 —— 小于指定时间单位的值会被视为相同, 比如对比月,则两个日期的 日/时/分/秒 会被认为相同,以此类推。

相差时间为正数表示date1日期晚于(大于)date2,负数相反,0表示时间/日期相同。

注意,如果对比单位是 h/m/s,务必要保持格式一致,比如

//实际相差8小时new Date('2020-01-01') //vsnew Date('2020/01/01')

Example

//0console.log(_.compareDate(new Date('2020/05/01'),'2020/5/1'))//格式不一致,相差8小时console.log(_.compareDate(new Date('2020-05-01'),'2020/5/1','h'))//-59console.log(_.compareDate(new Date('2019/01/01'),'2019/3/1'))

Parameters

NameTypeDescription
date1string | number | Date日期对象、时间戳或合法格式的日期时间字符串。 对于字符串格式,可以时UTC格式,或者 RFC2822格式
date2string | number | Date同date1
type?string比较时间单位
  • y
  • M
  • d
  • h
  • m
  • s

Returns

number

根据比较时间单位返回的比较值。正数为date1日期晚于(大于)date2,负数相反,0表示相同。


formatDate

formatDate(val, pattern?): string

通过表达式格式化日期时间

yyyy-MM-dd hh:mm:ss => 2020-12-11 10:09:08

pattern解释:

  • yy 2位年 - 22
  • yyyy 4位年 - 2022
  • M 1位月(1-12)
  • MM 2位月(01-12)
  • MMM 月描述(一月 - 十二月)
  • d 1位日(1-30/31/29/28)
  • dd 2位日(01-30/31/29/28)
  • ddd 一年中的日(1-365)
  • dddd 一年中的日(001-365)
  • h 1位小时(0-23)
  • hh 2位小时(00-23)
  • m 1位分钟(0-59)
  • mm 2位分钟(00-59)
  • s 1位秒(0-59)
  • ss 2位秒(00-59)
  • Q 季度(1-4)
  • QQ 季度描述(春-冬)
  • W 一年中的周(1-53)
  • WW 一年中的周(01-53)
  • w 一月中的周(1-6)
  • ww 一月中的周描述(第一周 - 第六周)
  • E 星期(1-7)
  • EE 星期描述(星期一 - 星期日)

Example

//now timeconsole.log(_.formatDate(_.now(),'yyyy-MM-dd hh:mm'))//2/1/2021console.log(_.formatDate('2021-2-1','M/d/yyyy'))//2/1/21console.log(_.formatDate('2021-2-1','M/d/yy'))//02/01/21console.log(_.formatDate('2021-2-1','MM/dd/yy'))//02/01/2021console.log(_.formatDate('2021-2-1','MM/dd/yyyy'))//21/02/01console.log(_.formatDate('2021-2-1','yy/MM/dd'))//2021-02-01console.log(_.formatDate('2021-2-1','yyyy-MM-dd'))//21-12-11 10:09:08console.log(_.formatDate('2021-12-11T10:09:08','yy-MM-dd hh:mm:ss'))//12/11/2020 1009console.log(_.formatDate('2020-12-11 10:09:08','MM/dd/yyyy hhmm'))//2020-12-11 08:00console.log(_.formatDate(1607644800000))//''console.log(_.formatDate('13:02'))//''console.log(_.formatDate(null))//现在时间:(20-12-11 10:09:08)console.log(_.formatDate('2020-12-11 10:09:08','现在时间:(yy-MM-dd hh:mm:ss)'))

Parameters

NameTypeDescription
valstring | number | Date需要格式化的值,可以是日期对象或时间字符串或日期毫秒数
pattern?string-

Returns

string

格式化后的日期字符串,无效日期返回空字符串


getDayOfYear

getDayOfYear(date): number

获取指定日期在当前年中的天数并返回

Parameters

NameTypeDescription
dateDate日期对象

Returns

number

当前年中的第几天


getWeekOfMonth

getWeekOfMonth(date): number

获取指定日期在当前月中的周数并返回

Parameters

NameTypeDescription
dateDate日期对象

Returns

number

当前月中的第几周


getWeekOfYear

getWeekOfYear(date): number

获取指定日期在当前年中的周数并返回

Parameters

NameTypeDescription
dateDate日期对象

Returns

number

当前年中的第几周


isLeapYear

isLeapYear(date): boolean

指定日期是否是闰年

Parameters

NameTypeDescription
dateDate日期对象

Returns

boolean

闰年返回true


isSameDay

isSameDay(date1, date2): boolean

比较两个日期是否为同一天

Example

//trueconsole.log(_.isSameDay(new Date('2020-05-01'),'2020/5/1'))//falseconsole.log(_.isSameDay(new Date('2020-05-01 23:59:59.999'),'2020/5/2 0:0:0.000'))

Parameters

NameTypeDescription
date1string | number | Date日期对象或合法格式的日期时间字符串
date2string | number | Date同date1

Returns

boolean


now

now(): number

返回13位日期毫秒数,表示从1970 年 1 月 1 日 00:00:00 (UTC)起到当前时间

Example

//now timeconsole.log(_.now())

Returns

number

带毫秒数的时间戳


toDate

toDate(value): Date

通过指定参数得到日期对象。支持多种签名

_.toDate(1320940800); //timestamp unix style_.toDate(1320940800123); //timestamp javascript style_.toDate([year,month,day]); //注意,month的索引从1开始_.toDate([year,month,day,hour,min,sec]); //注意,month的索引从1开始_.toDate(datetimeStr);

Example

//'2011/11/11 00:00:00'console.log(_.toDate(1320940800).toLocaleString())//'2011/11/11 00:01:39'console.log(_.toDate(1320940899999).toLocaleString())//'2022/12/12 00:00:00'console.log(_.toDate([2022,11,12]).toLocaleString())//'2022/12/12 12:12:12'console.log(_.toDate([2022,11,12,12,12,12]).toLocaleString())//'2022/2/2 00:00:00'console.log(_.toDate('2022/2/2').toLocaleString())//'2022/2/2 08:00:00'console.log(_.toDate('2022-02-02').toLocaleString())

Parameters

NameTypeDescription
valuestring | number | Date | number转换参数

Returns

Date

转换后的日期。无效日期统一返回1970/1/1