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
| Name | Type | Description |
|---|---|---|
date | string | number | Date | 原日期时间 |
amount | number | 变化量,可以为负数 |
type? | string | 量变时间类型
|
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
| Name | Type | Description |
|---|---|---|
date1 | string | number | Date | 日期对象、时间戳或合法格式的日期时间字符串。 对于字符串格式,可以时UTC格式,或者 RFC2822格式 |
date2 | string | number | Date | 同date1 |
type? | string | 比较时间单位
|
Returns
number
根据比较时间单位返回的比较值。正数为date1日期晚于(大于)date2,负数相反,0表示相同。
formatDate
▸ formatDate(val, pattern?): string
通过表达式格式化日期时间
yyyy-MM-dd hh:mm:ss => 2020-12-11 10:09:08pattern解释:
yy2位年 - 22yyyy4位年 - 2022M1位月(1-12)MM2位月(01-12)MMM月描述(一月 - 十二月)d1位日(1-30/31/29/28)dd2位日(01-30/31/29/28)ddd一年中的日(1-365)dddd一年中的日(001-365)h1位小时(0-23)hh2位小时(00-23)m1位分钟(0-59)mm2位分钟(00-59)s1位秒(0-59)ss2位秒(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
| Name | Type | Description |
|---|---|---|
val | string | number | Date | 需要格式化的值,可以是日期对象或时间字符串或日期毫秒数 |
pattern? | string | - |
Returns
string
格式化后的日期字符串,无效日期返回空字符串
getDayOfYear
▸ getDayOfYear(date): number
获取指定日期在当前年中的天数并返回
Parameters
| Name | Type | Description |
|---|---|---|
date | Date | 日期对象 |
Returns
number
当前年中的第几天
getWeekOfMonth
▸ getWeekOfMonth(date): number
获取指定日期在当前月中的周数并返回
Parameters
| Name | Type | Description |
|---|---|---|
date | Date | 日期对象 |
Returns
number
当前月中的第几周
getWeekOfYear
▸ getWeekOfYear(date): number
获取指定日期在当前年中的周数并返回
Parameters
| Name | Type | Description |
|---|---|---|
date | Date | 日期对象 |
Returns
number
当前年中的第几周
isLeapYear
▸ isLeapYear(date): boolean
指定日期是否是闰年
Parameters
| Name | Type | Description |
|---|---|---|
date | Date | 日期对象 |
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
| Name | Type | Description |
|---|---|---|
date1 | string | number | Date | 日期对象或合法格式的日期时间字符串 |
date2 | string | 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
| Name | Type | Description |
|---|---|---|
value | string | number | Date | number | 转换参数 |
Returns
Date
转换后的日期。无效日期统一返回1970/1/1
