Skip to content

DateTime#

DateTime.day#

Description: 月份中的日期(1-31)

Syntax: DateTime.day

Returns: Number

Type: Luxon

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.day //=> 30

DateTime.diffTo()#

Description: 返回两个 DateTime 之间的差值,以指定的单位表示

Syntax: DateTime.diffTo(otherDateTime, unit)

Returns: Number

Source: Custom n8n functionality

Parameters:

  • otherDateTime (String|DateTime) - 用于与基础 DateTime 相减的时间点。可以是 ISO 日期字符串或 Luxon DateTime。
  • unit (String|Array) - 可选 - 返回结果的单位或单位数组。可选值:yearsmonthsweeksdayshoursminutessecondsmilliseconds

Examples:

1
2
// dt1 = "2024-03-30T18:49:07.234".toDateTime()
dt1.diffTo('2025-01-01', 'days') //=> 276.21
1
2
3
// dt1 = "2024-03-30T18:49:07.234".toDateTime()
// dt2 = "2025-01-01T00:00:00.000".toDateTime()
dt1.diffTo(dt2, ['months', 'days']) //=> {'months':, 'days':}
1
Note: should support both day and days, etc.

DateTime.diffToNow()#

Description: 返回当前时刻与该 DateTime 之间的差值,以指定的单位表示。如需文本形式的表示,请使用 toRelative()

Syntax: DateTime.diffToNow(unit)

Returns: Number

Source: Custom n8n functionality

Parameters:

  • unit (String|Array) - 可选 - 返回结果的单位或单位数组。可选值:yearsmonthsweeksdayshoursminutessecondsmilliseconds

Examples:

1
2
// dt = "2023-03-30T18:49:07.234".toDateTime()
dt.diffToNow('days') //=> 371.9
1
2
// dt = "2023-03-30T18:49:07.234".toDateTime()
dt.diffToNow(['months', 'days']) //=> {"months":12, "days":5.9}
1
Note: should support both day and days, etc.

DateTime.endOf()#

Description: 将 DateTime 向上取整到某个单位的末尾,例如月末

Syntax: DateTime.endOf(unit, opts)

Returns: DateTime

Type: Luxon

Parameters:

  • unit (String) - 要取整到末尾的单位。可以是 yearquartermonthweekdayhourminutesecondmillisecond
  • opts (Object) - 可选 - 包含影响输出选项的对象。可用属性: useLocaleWeeks (boolean):是否在计算一周的开始时使用区域设置。默认为 false。

Examples:

1
2
// dt = "2024-03-20T18:49".toDateTime()
dt.endOf('month') //=> 2024-03-31T23:59

DateTime.equals()#

Description: 如果两个 DateTime 表示完全相同的时刻且处于相同的时区,则返回 true。如需不太严格的比较,请使用 hasSame()

Syntax: DateTime.equals(other)

Returns: Boolean

Type: Luxon

Parameters:

  • other (DateTime) - 要比较的另一个 DateTime

Examples:

1
2
3
// dt1 = "2024-03-20T18:49+01:00".toDateTime()
// dt2 = "2024-03-20T19:49+02:00".toDateTime()
dt1.equals(dt2) //=> false

DateTime.extract()#

Description: 提取日期或时间的某个部分(如月份),以数字形式返回。如需提取文本名称,请参见 format()

Syntax: DateTime.extract(unit?)

Returns: Number

Source: Custom n8n functionality

Parameters:

  • unit (String) - 可选 - 要返回的日期或时间部分。可选值:yearmonthweekdayhourminutesecond

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.extract('month') //=> 3
1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.extract('hour') //=> 18

DateTime.format()#

Description: 使用指定的格式将 DateTime 转换为字符串。格式化指南。对于常见格式,toLocaleString() 可能更简便。

Syntax: DateTime.format(fmt)

Returns: String

Source: Custom n8n functionality

Parameters:

  • fmt (String) - 返回字符串的格式

Examples:

1
2
// dt = "2024-04-30T18:49".toDateTime()
dt.format('dd/LL/yyyy') //=> '30/04/2024'
1
2
3
4
// dt = "2024-04-30T18:49".toDateTime()
dt.format('dd LLL yy') //=> '30 Apr 24'
dt.setLocale('fr').format('dd LLL yyyy') //=> '30 avr. 2024'
dt.format("HH 'hours and' mm 'minutes'") //=> '18 hours and 49 minutes'

DateTime.hasSame()#

Description: 如果两个 DateTime 在指定的单位精度下相同,则返回 true。时区会被忽略(仅比较本地时间),如有需要请先使用 toUTC()

Syntax: DateTime.hasSame(otherDateTime, unit)

Returns: Boolean

Type: Luxon

Parameters:

  • otherDateTime (DateTime) - 要比较的另一个 DateTime
  • unit (String) - 要检查的时间单位精度。可选值:yearquartermonthweekdayhourminutesecondmillisecond

Examples:

1
2
3
// dt1 = "2024-03-20".toDateTime()
// dt2 = "2024-03-18".toDateTime()
dt1.hasSame(dt2, 'month') //=> true
1
2
3
// dt1 = "1982-03-20".toDateTime()
// dt2 = "2024-03-18".toDateTime()
dt1.hasSame(dt2, 'month') //=> false

DateTime.hour#

Description: 一天中的小时(0-23)

Syntax: DateTime.hour

Returns: Number

Type: Luxon

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.hour //=> 18

DateTime.isBetween()#

Description: 如果该 DateTime 位于指定的两个时间点之间,则返回 true

Syntax: DateTime.isBetween(date1, date2)

Returns: Boolean

Source: Custom n8n functionality

Parameters:

  • date1 (String|DateTime) - 基础 DateTime 必须晚于的时间点。可以是 ISO 日期字符串或 Luxon DateTime。
  • date2 (String|DateTime) - 基础 DateTime 必须早于的时间点。可以是 ISO 日期字符串或 Luxon DateTime。

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.isBetween('2020-06-01', '2025-06-01') //=> true
1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.isBetween('2020', $now) //=> true

DateTime.isInDST#

Description: 该 DateTime 是否处于夏令时

Syntax: DateTime.isInDST

Returns: Boolean

Type: Luxon

DateTime.locale#

Description: DateTime 的区域设置,如 'en-GB'。区域设置用于格式化 DateTime。

Syntax: DateTime.locale

Returns: String

Type: Luxon

Examples:

1
$now.locale //=> 'en-US'

DateTime.millisecond#

Description: 秒中的毫秒(0-999)

Syntax: DateTime.millisecond

Returns: Number

Type: Luxon

Examples:

1
2
// dt = "2024-03-30T18:49:07.234".toDateTime()
dt.millisecond //=> 234

DateTime.minus()#

Description: 从 DateTime 中减去指定的时间段

Syntax: DateTime.minus(n, unit?)

Returns: DateTime

Source: Custom n8n functionality

Parameters:

  • n (Number|Object) - 要减去的单位数量。也可以使用 Luxon Duration 对象一次减去多个单位。
  • unit (String) - 可选 - 数字的单位。可选值:yearsmonthsweeksdayshoursminutessecondsmilliseconds

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.minus(7, 'days') //=> 2024-04-23T18:49
1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.minus(4, 'years') //=> 2020-04-30T18:49

DateTime.minute#

Description: 小时中的分钟(0-59)

Syntax: DateTime.minute

Returns: Number

Type: Luxon

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.minute //=> 49

DateTime.month#

Description: 月份(1-12)

Syntax: DateTime.month

Returns: Number

Type: Luxon

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.month //=> 3

DateTime.monthLong#

Description: 月份的完整文本名称,例如 'October'。如果未指定区域设置,则默认使用系统区域设置。

Syntax: DateTime.monthLong

Returns: String

Type: Luxon

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.monthLong //=> 'March'
1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.setLocale('de-DE').monthLong //=> 'März'

DateTime.monthShort#

Description: 月份的缩写文本名称,例如 'Oct'。如果未指定区域设置,则默认使用系统区域设置。

Syntax: DateTime.monthShort

Returns: String

Type: Luxon

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.monthShort //=> 'Mar'
1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.setLocale('de-DE').monthShort //=> 'Mär'

DateTime.plus()#

Description: 向 DateTime 添加指定的时间段

Syntax: DateTime.plus(n, unit?)

Returns: DateTime

Source: Custom n8n functionality

Parameters:

  • n (Number|Object) - 要添加的单位数量。也可以使用 Luxon Duration 对象一次添加多个单位。
  • unit (String) - 可选 - 数字的单位。可选值:yearsmonthsweeksdayshoursminutessecondsmilliseconds

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.plus(7, 'days') //=> 2024-05-07T18:49
1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.plus(4, 'years') //=> 2028-04-30T18:49

DateTime.quarter#

Description: 一年中的季度(1-4)

Syntax: DateTime.quarter

Returns: Number

Type: Luxon

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.quarter //=> 1

DateTime.second#

Description: 分钟中的秒(0-59)

Syntax: DateTime.second

Returns: Number

Type: Luxon

Examples:

1
2
// dt = "2024-03-30T18:49:07.234".toDateTime()
dt.second //=> 7

DateTime.set()#

Description: 为 DateTime 的指定单位赋予新值。如需对 DateTime 取整,另请参阅 startOf()endOf()

Syntax: DateTime.set(values)

Returns: DateTime

Type: Luxon

Parameters:

  • values (Object) - 包含要设置的单位和对应值的对象。可用键名:yearmonthdayhourminutesecondmillsecond

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.set({year:1982, month:10}) //=> 1982-10-20T18:49

DateTime.setLocale()#

Description: 设置区域设置,用于确定 DateTime 的语言和格式。在生成 DateTime 的文本表示时很有用,例如使用 format()toLocaleString()

Syntax: DateTime.setLocale(locale)

Returns: DateTime

Type: Luxon

Parameters:

  • locale (String) - 要指定的区域设置,例如 'en-GB'(英式英语)或 'pt-BR'(巴西葡萄牙语)。列表(非官方)

Examples:

1
$now.setLocale('de-DE').toLocaleString({'dateStyle':'long'}) //=> 5. Oktober 2024
1
$now.setLocale('fr-FR').toLocaleString({'dateStyle':'long'}) //=> 5 octobre 2024

DateTime.setZone()#

Description: 将 DateTime 转换为指定的时区。除非在选项中另行指定,DateTime 仍然表示相同的时刻。另请参阅 toLocal()toUTC()

Syntax: DateTime.setZone(zone, opts)

Returns: DateTime

Type: Luxon

Parameters:

  • zone (String) - 可选 - 时区标识符,格式为 'America/New_York'、'UTC+3',或字符串 'local' 或 'utc'
  • opts (Object) - 可选 - 影响输出的选项。可用属性: keepCalendarTime (boolean):是否保持时间不变而仅更改偏移量。默认为 false。

Examples:

1
2
// dt = "2024-01-01T00:00:00.000+02:00".toDateTime()
dt.setZone('America/Buenos_aires') //=> 2023-12-31T19:00:00.000-03:00
1
2
// dt = "2024-01-01T00:00:00.000+02:00".toDateTime()
dt.setZone('UTC+7') //=> 2024-01-01T05:00:00.000+07:00

DateTime.startOf()#

Description: 将 DateTime 向下取整到某个单位的开始,例如月初

Syntax: DateTime.startOf(unit, opts)

Returns: DateTime

Type: Luxon

Parameters:

  • unit (String) - 要取整到开始的单位。可选值:yearquartermonthweekdayhourminutesecondmillisecond
  • opts (Object) - 可选 - 包含影响输出选项的对象。可用属性: useLocaleWeeks (boolean):是否在计算一周的开始时使用区域设置。默认为 false。

Examples:

1
2
// dt = "2024-03-20T18:49".toDateTime()
dt.startOf('month') //=> 2024-03-01T00:00

DateTime.toISO()#

Description: 返回 DateTime 的 ISO 8601 标准字符串表示

Syntax: DateTime.toISO(opts)

Returns: String

Type: Luxon

Parameters:

  • opts (Object) - 可选 - 配置选项。详见 Luxon 文档

Examples:

1
$now.toISO() //=> 2024-04-05T18:44:55.525+02:00

DateTime.toLocal()#

Description: 将 DateTime 转换为工作流的本地时区。除非在参数中另行指定,DateTime 仍然表示相同的时刻。工作流的时区可以在工作流设置中更改。

Syntax: DateTime.toLocal()

Returns: DateTime

Type: Luxon

Examples:

1
2
// dt = "2024-01-01T00:00:00.000Z".toDateTime()
dt.toLocal() //=> 2024-01-01T01:00:00.000+01:00, if time zone is Europe/Berlin

DateTime.toLocaleString()#

Description: 返回 DateTime 的本地化字符串表示,即使用其区域设置对应的语言和格式。如果未指定区域设置,则默认使用系统区域设置。

Syntax: DateTime.toLocaleString(formatOpts)

Returns: String

Type: Luxon

Parameters:

  • formatOpts (Object) - 可选 - 渲染的配置选项。完整列表请参见 Intl.DateTimeFormat。默认渲染短日期格式。

Examples:

1
2
3
$now.toLocaleString() //=> '4/30/2024'
$now.toLocaleString({'dateStyle':'medium', 'timeStyle':'short'}) //=> 'Apr 30, 2024, 10:00 PM'
// (if in US English locale)
1
$now.setLocale('de-DE').toLocaleString() //=> '30.4.2024'
1
2
3
4
5
$now.toLocaleString({'dateStyle':'short'}) //=> '4/30/2024'
$now.toLocaleString({'dateStyle':'medium'}) //=> 'Apr 30, 2024'
$now.toLocaleString({'dateStyle':'long'}) //=> 'April 30, 2024'
$now.toLocaleString({'dateStyle':'full'}) //=> 'Tuesday, April 30, 2024'
// (if in US English locale)
1
2
3
4
5
$now.toLocaleString({'year':'numeric', 'month':'numeric', 'day':'numeric'}) //=> '4/30/2024'
$now.toLocaleString({'year':'2-digit', 'month':'2-digit', 'day':'2-digit'}) //=> '04/30/24'
$now.toLocaleString({'month':'short', 'weekday':'short', 'day':'numeric'}) //=> 'Tue, Apr 30'
$now.toLocaleString({'month':'long', 'weekday':'long', 'day':'numeric'}) //=> 'Tuesday, April 30'
// (if in US English locale)
1
2
3
4
5
$now.toLocaleString({'timeStyle':'short'}) //=> '10:00 PM'
$now.toLocaleString({'timeStyle':'medium'}) //=> '10:00:58 PM'
$now.toLocaleString({'timeStyle':'long'}) //=> '10:00:58 PM GMT+2'
$now.toLocaleString({'timeStyle':'full'}) //=> '10:00:58 PM Central European Summer Time'
// (if in US English locale)
1
2
3
$now.toLocaleString({'hour':'numeric', 'minute':'numeric', hourCycle:'h24'}) //=> '22:00'
$now.toLocaleString({'hour':'2-digit', 'minute':'2-digit', hourCycle:'h12'}) //=> '10:00 PM'
// (if in US English locale)

DateTime.toMillis()#

Description: 返回以毫秒为单位的 Unix 时间戳(自 1970 年 1 月 1 日以来经过的毫秒数)

Syntax: DateTime.toMillis()

Returns: Number

Type: Luxon

Examples:

1
$now.toMillis() //=> 1712334324677

DateTime.toRelative()#

Description: 返回相对于当前时间的文本表示,例如 'in two days'。默认向下取整。

Syntax: DateTime.toRelative(options)

Returns: String

Type: Luxon

Parameters:

  • options (Object) - 可选 - 影响输出的选项。可用属性: unit = 默认使用的单位(yearsmonthsdays 等)。 locale = 使用的语言和格式(例如 defr

Examples:

1
$now.plus(1, 'day').toRelative() //=> "in 1 day"
1
$now.plus(1, 'day').toRelative({unit:'hours'}) //=> "in 24 hours"
1
$now.plus(1, 'day').toRelative({locale:'es'}) //=> "dentro de 1 día"

DateTime.toSeconds()#

Description: 返回以秒为单位的 Unix 时间戳(自 1970 年 1 月 1 日以来经过的秒数)

Syntax: DateTime.toSeconds()

Returns: Number

Type: Luxon

Examples:

1
$now.toSeconds() //=> 1712334442.372

DateTime.toString()#

Description: 返回 DateTime 的字符串表示。类似于 toISO()。如需更多格式化选项,请参见 format()toLocaleString()

Syntax: DateTime.toString()

Returns: string

Type: Luxon

Examples:

1
$now.toString() //=> 2024-04-05T18:44:55.525+02:00

DateTime.toUTC()#

Description: 将 DateTime 转换为 UTC 时区。除非在参数中另行指定,DateTime 仍然表示相同的时刻。使用 setZone() 可转换到其他时区。

Syntax: DateTime.toUTC(offset, opts)

Returns: DateTime

Type: Luxon

Parameters:

  • offset (Number) - 可选 - 相对于 UTC 的偏移量(以分钟为单位)
  • opts (Object) - 可选 - 包含影响输出选项的对象。可用属性: keepCalendarTime (boolean):是否保持时间不变而仅更改偏移量。默认为 false。

Examples:

1
2
// dt = "2024-01-01T00:00:00.000+02:00".toDateTime()
dt.toUTC() //=> 2023-12-31T22:00:00.000Z

DateTime.weekday#

Description: 一周中的第几天。1 表示星期一,7 表示星期日。

Syntax: DateTime.weekday

Returns: Number

Type: Luxon

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.weekday //=> 6

DateTime.weekdayLong#

Description: 星期几的完整文本名称,例如 'Wednesday'。如果未指定区域设置,则默认使用系统区域设置。

Syntax: DateTime.weekdayLong

Returns: String

Type: Luxon

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.weekdayLong //=> 'Saturday'
1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.setLocale('de-DE').weekdayLong //=> 'Samstag'

DateTime.weekdayShort#

Description: 星期几的缩写文本名称,例如 'Wed'。如果未指定区域设置,则默认使用系统区域设置。

Syntax: DateTime.weekdayShort

Returns: String

Type: Luxon

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.weekdayShort //=> 'Sat'
1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.setLocale('fr-FR').weekdayShort //=> 'sam.'

DateTime.weekNumber#

Description: 一年中的第几周(1-52 左右)

Syntax: DateTime.weekNumber

Returns: Number

Type: Luxon

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.weekNumber //=> 13

DateTime.year#

Description: 年份

Syntax: DateTime.year

Returns: Number

Type: Luxon

Examples:

1
2
// dt = "2024-03-30T18:49".toDateTime()
dt.year //=> 2024

DateTime.zone#

Description: 与该 DateTime 关联的时区

Syntax: DateTime.zone

Returns: Object

Type: Luxon

Examples:

1
$now.zone //=> {"zoneName": "Europe/Berlin", "valid": true}