Skip to content

Date日期

概述

日期运算的。包括有关月份和星期的值范围的信息,以及几种用于处理日期的方法,如Date.sysdateDate.nowDate.dateSet 及有关月份和星期值范围的特殊说明

特殊说明

month的值范围

月份
1月0
2月1
3月2
4月3
5月4
6月5
7月6
8月7
9月8
10月9
11月10
12月11

day_of_week的值范围

星期日0
星期一1
星期二2
星期三3
星期四4
星期五5
星期六6

sysdate

返回当前日期和时间

javascript
Date.sysdate()

返回值

类型 Date 当前的日期和时间

示例

javascript
Date.sysdate() //Date 对象

now

返回当前时间的UNIX时间戳

javascript
Date.now()

返回值

类型 Long 当前时间的UNIX时间戳

示例

javascript
Date.now() // 1668483800328

dateSet

将日期d中type指定的部分设置为value

javascript
Date.dateSet(d, type, value)
参数类型描述
dDateLong需要计算的日期或UNIX时间戳
typeString运算类型
valueInteger设置的值,如果为 null 则默认0 开始

INFO

type的取值为:年:year,月:month,天:day_of_year,月天数:day_of_month,周天数:day_of_week,小时:hour,分钟:minute,秒:second,毫秒:millisecond

返回值

类型 Date 计算后的日期

示例

javascript
//假设date的值为2022-11-01 00:00:00,000,时间戳 timestamp 为 1667232000000
Date.dateSet(date, 'year', 2024); //2024-11-01 00:00:00,000
Date.dateSet(date, 'year', null); //0001-11-01 00:00:00,000
Date.dateSet(date, 'year', -1); //0002-11-01 00:00:00,000
Date.dateSet(null, 'year', 2024); //null
Date.dateSet(date, null, 2024); //null
Date.dateSet(date, 'month', 1); //2022-02-01 00:00:00,000
Date.dateSet(date, 'day_of_year', 1); //2022-01-01 00:00:00,000
Date.dateSet(date, 'day_of_month', 1); //2022-11-01 00:00:00,000
Date.dateSet(date, 'day_of_week', 1); //2022-10-30 00:00:00,000
Date.dateSet(date, 'hour', 1); //2022-11-01 01:00:00,000
Date.dateSet(date, 'minute', 1); //2022-11-01 00:01:00,000
Date.dateSet(date, 'second', 1); //2022-11-01 00:00:01,000
Date.dateSet(date, 'millisecond', 1); //2022-11-01 00:00:00,001
Date.dateSet(timestamp, 'year', 2024); //2024-11-01 00:00:00,000
Date.dateSet(timestamp, 'month', 1); //2022-02-01 00:00:00,000

dateAdd

计算日期d按照类型type加上diff的日期

javascript
Date.dateAdd(d, type, diff)
参数类型描述
dDate或Long需要计算的日期或UNIX时间戳
typeString运算类型
diffInteger增加或者减少的值,如果为 null 则默认0 开始。增加则传递整数,减少则传递负数

INFO

type的取值为:年:year,月:month,天:day_of_year,月天数:day_of_month,周天数:day_of_week,小时:hour,分钟:minute,秒:second,毫秒:millisecond

返回值

类型 Date 计算后的日期

示例

javascript
//假设date的值为2022-11-01 00:00:00,000,时间戳 timestamp 为 1667232000000
Date.dateAdd(date, 'year', 1); //2023-11-01 00:00:00,000
Date.dateAdd(date, 'year', null); //2022-11-01 00:00:00,000
Date.dateAdd(date, 'year', -1); //2021-11-01 00:00:00,000
Date.dateAdd(date, 'month', 1); //2022-12-01 00:00:00,000
Date.dateAdd(date, 'day_of_year', 1); //2022-11-02 00:00:00,000
Date.dateAdd(date, 'day_of_month', 1); //2022-11-02 00:00:00,000
Date.dateAdd(date, 'day_of_week', 1); //2022-11-02 00:00:00,000
Date.dateAdd(date, 'hour', 1); //2022-11-01 01:00:00,000
Date.dateAdd(date, 'minute', 1); //2022-11-01 00:01:00,000
Date.dateAdd(date, 'second', 1); //2022-11-01 00:00:01,000
Date.dateAdd(date, 'millisecond', 1); //2022-11-01 00:00:00,001
Date.dateAdd(timestamp, 'year', 2); //2024-11-01 00:00:00,000
Date.dateAdd(timestamp, 'month', 1); //2022-12-01 00:00:00,000

datePart

返回日期d中的type指定的部分

javascript
Date.datePart(d, type)
参数类型描述
dDateLong需要计算的日期或UNIX时间戳
typeString运算类型

INFO

type的取值为:年:year,月:month,天:day_of_year,月天数:day_of_month,周天数:day_of_week,小时:hour,分钟:minute,秒:second,毫秒:millisecond

返回值

类型 Integer 日期d中type指定的部分

示例

javascript
//假设date的值为2022-11-01 13:10:12,000,时间戳 timestamp 为 1667279412000
Date.datePart(date, 'year'); //2022
Date.datePart(date, 'month'); //10
Date.datePart(date, 'day_of_year'); //305
Date.datePart(date, 'day_of_month'); //1
Date.datePart(date, 'day_of_week'); //3
Date.datePart(date, 'hour'); //13
Date.datePart(date, 'minute'); //10
Date.datePart(date, 'second'); //12
Date.datePart(date, 'millisecond'); //0
Date.datePart(null, 'year'); //null
Date.datePart(date, null); //null
Date.datePart(timestamp, 'year'); //2022
Date.datePart(timestamp, 'month'); //10
Date.datePart(date, 'week_of_year'); //45

dateBefore

判定日期d1是否在日期d2之前

javascript
Date.dateBefore(d1, d2)
参数类型描述
d1DateLong日期1或UNIX时间戳
d2DateLong日期2或UNIX时间戳

返回值

类型 Boolean 日期d1是否在日期d2之前,如果d1和d2相等返回false

示例

javascript
//假设date1的值为 2022-11-01 13:10:12,000,时间戳 timestamp 为 1667279412000
//假设date2的值为 2022-02-02 13:10:12,000,时间戳 timestamp 为 1643778612000

Date.dateBefore(date1, date2); //false
Date.dateBefore(date2, date1); //true
Date.dateBefore(timestamp1, timestamp2); //false
Date.dateBefore(timestamp2, timestamp1); //true
Date.dateBefore(timestamp1, date2); //false
Date.dateBefore(timestamp2, date1); //true
Date.dateBefore(date1, null); //false
Date.dateBefore(null, date2); //false
Date.dateBefore(timestamp1, null); //false
Date.dateBefore(null, timestamp2); //false
Date.dateBefore(null, null); //false

dateAfter

判定日期d1是否在日期d2之后

javascript
Date.dateAfter(d1, d2)
参数类型描述
d1DateLong日期1或UNIX时间戳
d2DateLong日期2或UNIX时间戳

返回值

类型 Boolean 日期d1是否在日期d2之后,如果d1和d2相等返回false

示例

javascript
//假设date1的值为 2022-11-01 13:10:12,000,时间戳 timestamp 为 1667279412000
//假设date2的值为 2022-02-02 13:10:12,000,时间戳 timestamp 为 1643778612000

Date.dateAfter(date1, date2); //true
Date.dateAfter(date2, date1); //false
Date.dateAfter(timestamp1, timestamp2); //true
Date.dateAfter(timestamp2, timestamp1); //false
Date.dateAfter(timestamp1, date2); //true
Date.dateAfter(timestamp2, date1); //false
Date.dateAfter(date1, null); //false
Date.dateAfter(null, date2); //false
Date.dateAfter(timestamp1, null); //false
Date.dateAfter(null, timestamp2); //false
Date.dateAfter(null, null); //false

dateDiff

计算两个日期之间的天数差值

javascript
Date.dateDiff(d1, d2)
参数类型描述
d1DateLong日期1或UNIX时间戳
d2DateLong日期2或UNIX时间戳

返回值

类型 Integer 计算两个日志之间相差的天数。相等则会0,若d1在d2之前,则返回负值,反之返回正值

示例

javascript
//假设date1的值为 2022-10-01 13:10:12,时间戳 timestamp 为 1667279412000
//假设date2的值为 2023-12-01 13:10:12,时间戳 timestamp 为 1701407412000

Date.dateDiff(date1, date2); //-426
Date.dateDiff(date2, date1); //426
Date.dateDiff(timestamp1, timestamp2); //-426
Date.dateDiff(timestamp2, timestamp1); //426
Date.dateDiff(timestamp1, date2); //-426
Date.dateDiff(timestamp2, date1); //426
Date.dateDiff(date1, null); //null
Date.dateDiff(null, date2); //null
Date.dateDiff(timestamp1, null); //null
Date.dateDiff(null, timestamp2); //null
Date.dateDiff(null, null); //null

monthDiff

计算两个日期之间的月份数差值

javascript
Date.monthDiff(d1, d2)
参数类型描述
d1DateLong日期1或UNIX时间戳
d2DateLong日期2或UNIX时间戳

返回值

类型 Integer 计算两个日志之间相差的月份数。相等则会0,若d1在d2之前,则返回负值,反之返回正值

示例

javascript
//假设date1的值为 2022-10-01 13:10:12,时间戳 timestamp 为 1667279412000
//假设date2的值为 2023-12-01 13:10:12,时间戳 timestamp 为 1701407412000

Date.monthDiff(date1, date2); //-14
Date.monthDiff(date2, date1); //14
Date.monthDiff(timestamp1, timestamp2); //-14
Date.monthDiff(timestamp2, timestamp1); //14
Date.monthDiff(timestamp1, date2); //-14
Date.monthDiff(timestamp2, date1); //14
Date.monthDiff(date1, null); //null
Date.monthDiff(null, date2); //null
Date.monthDiff(timestamp1, null); //null
Date.monthDiff(null, timestamp2); //null
Date.monthDiff(null, null); //null

weekDiff

计算两个日期之间的周差值

javascript
Date.weekDiff(d1, d2)
参数类型描述
d1DateLong日期1或UNIX时间戳
d2DateLong日期2或UNIX时间戳

返回值

类型 Integer 计算两个日志之间相差的周数。相等则会0,若d1在d2之前,则返回负值,反之返回正值

示例

javascript
//假设date1的值为 2022-10-01 13:10:12,时间戳 timestamp 为 1667279412000
//假设date2的值为 2023-12-01 13:10:12,时间戳 timestamp 为 1701407412000

Date.weekDiff(date1, date2); //-60
Date.weekDiff(date2, date1); //60
Date.weekDiff(timestamp1, timestamp2); //-60
Date.weekDiff(timestamp2, timestamp1); //60
Date.weekDiff(timestamp1, date2); //-60
Date.weekDiff(timestamp2, date1); //60
Date.weekDiff(date1, null); //null
Date.weekDiff(null, date2); //null
Date.weekDiff(timestamp1, null); //null
Date.weekDiff(null, timestamp2); //null
Date.weekDiff(null, null); //null

quarterDiff

计算两个日期之间的季度差值

javascript
Date.quarterDiff(d1, d2)
参数类型描述
d1DateLong日期1或UNIX时间戳
d2DateLong日期2或UNIX时间戳

返回值

类型 Integer 计算两个日志之间相差的季度数。相等则会0,若d1在d2之前,则返回负值,反之返回正值

示例

javascript
//假设date1的值为 2022-10-01 13:10:12,时间戳 timestamp 为 1667279412000
//假设date2的值为 2023-12-01 13:10:12,时间戳 timestamp 为 1701407412000

Date.quarterDiff(date1, date2); //-4
Date.quarterDiff(date2, date1); //4
Date.quarterDiff(timestamp1, timestamp2); //-4
Date.quarterDiff(timestamp2, timestamp1); //4
Date.quarterDiff(timestamp1, date2); //-4
Date.quarterDiff(timestamp2, date1); //4
Date.quarterDiff(date1, null); //null
Date.quarterDiff(null, date2); //null
Date.quarterDiff(timestamp1, null); //null
Date.quarterDiff(null, timestamp2); //null
Date.quarterDiff(null, null); //null