日期时间
DateTime.day ‘日’
Description: 月份中的日期(1-31)
Syntax: 语法: DateTime.day
Returns: 返回: 编号
Type: 类型: 勒克森
Examples: 示例:
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.day //=> 30
DateTime.diffTo()
Description: 返回两个 DateTime 之间的差值,以指定的单位表示
Syntax: 语法: DateTime.diffTo(otherDateTime, 单位)
Returns: 返回: 编号
Source: 来源: 自定义 n8n 功能
Parameters: 参数:
otherDateTime(String|DateTime) - 用于与基础 DateTime 相减的时间点。可以是 ISO 日期字符串或 Luxon DateTime。unit(String|Array) - 可选 - 返回结果的单位或单位数组。可选值: years、months、weeks、days、hours、minutes、seconds、milliseconds。
Examples: 示例:
javascript
// dt1 = "2024-03-30T18:49:07.234".toDateTime()
dt1.diffTo('2025-01-01', 'days') //=> 276.21
javascript
// dt1 = "2024-03-30T18:49:07.234".toDateTime()
// dt2 = "2025-01-01T00:00:00.000".toDateTime()
dt1.diffTo(dt2, ['months', 'days']) //=> {'months':, 'days':}
javascript
Note: should support both day and days, etc.
DateTime.diffToNow()
Description: 返回当前时刻与该 DateTime 之间的差值,以指定的单位表示。如需文本形式的表示,请使用 toRelative()。
Syntax: 语法: DateTime.diffToNow(单位)
Returns: 返回: 编号
Source: 来源: 自定义 n8n 功能
Parameters: 参数:
unit(String|Array) - 可选 - 返回结果的单位或单位数组。可选值: years、months、weeks、days、hours、minutes、seconds、milliseconds。
Examples: 示例:
javascript
// dt = "2023-03-30T18:49:07.234".toDateTime()
dt.diffToNow('days') //=> 371.9
javascript
// dt = "2023-03-30T18:49:07.234".toDateTime()
dt.diffToNow(['months', 'days']) //=> {"months":12, "days":5.9}
javascript
Note: should support both day and days, etc.
DateTime.endOf() 结束()
Description: 将 DateTime 向上取整到某个单位的末尾,例如月末
Syntax: 语法: DateTime.endOf(单位,选项)
Returns: 返回: 日期时间
Type: 类型: 勒克森
Parameters: 参数:
unit(String) - 要取整到末尾的单位。可以是year、quarter、month、week、day、hour、minute、second或millisecond。opts(Object) - 可选 - 包含影响输出选项的对象。可用属性:useLocaleWeeks(boolean):是否在计算一周的开始时使用区域设置。默认为 false。
Examples: 示例:
javascript
// dt = "2024-03-20T18:49".toDateTime()
dt.endOf('month') //=> 2024-03-31T23:59
DateTime.equals() 等于()
Description: 如果两个 DateTime 表示完全相同的时刻且处于相同的时区,则返回 true。如需不太严格的比较,请使用 hasSame()。
Syntax: 语法: DateTime.等于(其他)
Returns: 返回: 布尔值
Type: 类型: 勒克森
Parameters: 参数:
other(DateTime) - 要比较的另一个 DateTime
Examples: 示例:
javascript
// 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.提取(单位?)
Returns: 返回: 编号
Source: 来源: 自定义 n8n 功能
Parameters: 参数:
unit(String) - 可选 - 要返回的日期或时间部分。可选值:year、month、week、day、hour、minute、second
Examples: 示例:
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.extract('month') //=> 3
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.extract('hour') //=> 18
DateTime.format() 格式()
Description: 使用指定的格式将 DateTime 转换为字符串。格式化指南。对于常见格式,toLocaleString() 可能更简便。
Syntax: 语法: DateTime.格式(fmt)
Returns: 返回: 字符串
Source: 来源: 自定义 n8n 功能
Parameters: 参数:
fmt(String) - 返回字符串的格式
Examples: 示例:
javascript
// dt = "2024-04-30T18:49".toDateTime()
dt.format('dd/LL/yyyy') //=> '30/04/2024'
javascript
// 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'
警告
Code 节点中不可用
format() 是 n8n 在表达式编辑器中为 Luxon DateTime 对象添加的自定义扩展。Code 节点脚本使用未经修改的原生 Luxon,因此该方法在 Code 节点中不存在。在其中调用此方法会抛出 ... .format is not a function 错误。
请改用 Luxon 原生的 toFormat():
// In the Code node
DateTime.now().toFormat('dd/LL/yyyy'); //=> '30/04/2024'
这同样适用于本页中标记为 Source: Custom n8n functionality 来源:n8n 自定义功能 的所有方法:它们可在表达式中使用(例如 Set 节点),但在 Code 节点中可能不可用。有关两者区别的更多信息,请参阅内置方法和变量。
DateTime.hasSame()
Description: 如果两个 DateTime 在指定的单位精度下相同,则返回 true。时区会被忽略(仅比较本地时间),如有需要请先使用 toUTC()。
Syntax: 语法: DateTime.hasSame(其他日期时间,单位)
Returns: 返回: 布尔值
Type: 类型: 勒克森
Parameters: 参数:
otherDateTime(DateTime) - 要比较的另一个 DateTimeunit(String) - 要检查的时间单位精度。可选值:year、quarter、month、week、day、hour、minute、second或millisecond。
Examples: 示例:
javascript
// dt1 = "2024-03-20".toDateTime()
// dt2 = "2024-03-18".toDateTime()
dt1.hasSame(dt2, 'month') //=> true
javascript
// dt1 = "1982-03-20".toDateTime()
// dt2 = "2024-03-18".toDateTime()
dt1.hasSame(dt2, 'month') //=> false
DateTime.hour 小时
Description: 一天中的小时(0-23)
Syntax: 语法: DateTime.小时
Returns: 返回: 编号
Type: 类型: 勒克森
Examples: 示例:
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.hour //=> 18
DateTime.isBetween()
Description: 如果该 DateTime 位于指定的两个时间点之间,则返回 true
Syntax: 语法: DateTime.isBetween(日期1,日期2)
Returns: 返回: 布尔值
Source: 来源: 自定义 n8n 功能
Parameters: 参数:
date1(String|DateTime) - 基础 DateTime 必须晚于的时间点。可以是 ISO 日期字符串或 Luxon DateTime。date2(String|DateTime) - 基础 DateTime 必须早于的时间点。可以是 ISO 日期字符串或 Luxon DateTime。
Examples: 示例:
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.isBetween('2020-06-01', '2025-06-01') //=> true
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.isBetween('2020', $now) //=> true
DateTime.isEmpty()
Description 描述: 对所有 DateTime 返回 false;对 null 或 undefined 返回 true。
Syntax 语法: DateTime.isEmpty()
Returns 返回: 布尔值
Source 来源: n8n 自定义功能
Examples 示例:
javascript
// dt = '2023-03-30T18:49:07.234'.toDateTime()
dt.isEmpty() // => false
javascript
// dt = null
dt.isEmpty() // => true
DateTime.isInDST
Description: 该 DateTime 是否处于夏令时
Syntax: 语法: DateTime.isInDST
Returns: 返回: 布尔值
Type: 类型: 勒克森
DateTime.isNotEmpty()
Description 描述: 对所有 DateTime 返回 true;对 null 或 undefined 返回 false。
Syntax 语法: DateTime.isNotEmpty()
Returns 返回: 布尔值
Source 来源: n8n 自定义功能
Examples 示例:
javascript
// dt = '2023-03-30T18:49:07.234'.toDateTime()
dt.isNotEmpty() // => true
javascript
// dt = null
dt.isNotEmpty() // => false
DateTime.locale
Description: DateTime 的区域设置,如 'en-GB'。区域设置用于格式化 DateTime。
Syntax: 语法: DateTime.locale
Returns: 返回: 字符串
Type: 类型: 勒克森
Examples: 示例:
javascript
$now.locale //=> 'en-US'
DateTime.millisecond 毫秒
Description: 秒中的毫秒(0-999)
Syntax: 语法: DateTime.毫秒
Returns: 返回: 编号
Type: 类型: 勒克森
Examples: 示例:
javascript
// dt = "2024-03-30T18:49:07.234".toDateTime()
dt.millisecond //=> 234
DateTime.minus() 减号()
Description: 从 DateTime 中减去指定的时间段
Syntax: 语法: DateTime.minus(n,单位?)
Returns: 返回: 日期时间
Source: 来源: 自定义 n8n 功能
Parameters: 参数:
n(Number|Object) - 要减去的单位数量。也可以使用 Luxon Duration 对象一次减去多个单位。unit(String) - 可选 - 数字的单位。可选值:years、months、weeks、days、hours、minutes、seconds、milliseconds
Examples: 示例:
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.minus(7, 'days') //=> 2024-04-23T18:49
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.minus(4, 'years') //=> 2020-04-30T18:49
提示
Code 节点:Luxon 原生的 DateTime.prototype.minus() 也可在 Code 节点中使用,但它只接受一个 Duration-like 对象(例如 dt.minus({ days: 7 })),不支持本页介绍的 n8n 专用 (n, unit) 简写。在 Code 节点中传入 dt.minus(7, 'days') 不会引发错误,但也不会减去 7 天——使用这种签名时,请仔细检查结果。
DateTime.minute ‘分钟’
Description: 小时中的分钟(0-59)
Syntax: 语法: DateTime.分钟
Returns: 返回: 编号
Type: 类型: 勒克森
Examples: 示例:
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.minute //=> 49
DateTime.month 月
Description: 月份(1-12)
Syntax: 语法:DateTime.月
Returns: 返回: 编号
Type: 类型: 勒克森
Examples: 示例:
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.month //=> 3
DateTime.monthLong 一个月
Description: 月份的完整文本名称,例如 'October'。如果未指定区域设置,则默认使用系统区域设置。
Syntax: 语法: DateTime.monthLong
Returns: 返回: 字符串
Type: 类型: 勒克森
Examples: 示例:
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.monthLong //=> 'March'
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.setLocale('de-DE').monthLong //=> 'März'
DateTime.monthShort 月短
Description: 月份的缩写文本名称,例如 'Oct'。如果未指定区域设置,则默认使用系统区域设置。
Syntax: 语法: DateTime.monthShort
Returns: 返回: 字符串
Type: 类型: 勒克森
Examples: 示例:
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.monthShort //=> 'Mar'
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.setLocale('de-DE').monthShort //=> 'Mär'
DateTime.plus() 加号()
Description: 向 DateTime 添加指定的时间段
Syntax: 语法: DateTime.plus(n, 单位?)
Returns: 返回: 日期时间
Source: 来源: 自定义 n8n 功能
Parameters: 参数:
n(Number|Object) - 要添加的单位数量。也可以使用 Luxon Duration 对象一次添加多个单位。unit(String) - 可选 - 数字的单位。可选值:years、months、weeks、days、hours、minutes、seconds、milliseconds
Examples: 示例:
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.plus(7, 'days') //=> 2024-04-06T18:49
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.plus(4, 'years') //=> 2028-04-30T18:49
提示
Code 节点:Luxon 原生的 DateTime.prototype.plus() 也可在 Code 节点中使用,但它只接受一个 Duration-like 对象(例如 dt.plus({ days: 7 })),不支持本页介绍的 n8n 专用 (n, unit) 简写。在 Code 节点中传入 dt.plus(7, 'days') 不会引发错误,但也不会增加 7 天。使用这种签名时,请仔细检查结果。
DateTime.quarter ‘四分之一’
Description: 一年中的季度(1-4)
Syntax: 语法:DateTime.四分之一
Returns: 返回: 编号
Type: 类型: 勒克森
Examples: 示例:
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.quarter //=> 1
DateTime.second ‘第二个’
Description: 分钟中的秒(0-59)
Syntax: 语法: DateTime.第二
Returns: 返回: 编号
Type: 类型: 勒克森
Examples: 示例:
javascript
// dt = "2024-03-30T18:49:07.234".toDateTime()
dt.second //=> 7
DateTime.set() 设置()
Description: 为 DateTime 的指定单位赋予新值。如需对 DateTime 取整,另请参阅 startOf() 和 endOf()。
Syntax: 语法: DateTime.set(值)
Returns: 返回: 日期时间
Type: 类型: 勒克森
Parameters: 参数:
values(Object) - 包含要设置的单位和对应值的对象。可用键名:year、month、day、hour、minute、second和millsecond。
Examples: 示例:
javascript
// 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(区域设置)
Returns: 返回: 日期时间
Type: 类型: 勒克森
Parameters: 参数:
locale(String) - 要指定的区域设置,例如 'en-GB'(英式英语)或 'pt-BR'(巴西葡萄牙语)。列表(非官方)
Examples: 示例:
javascript
$now.setLocale('de-DE').toLocaleString({'dateStyle':'long'}) //=> 5. Oktober 2024
javascript
$now.setLocale('fr-FR').toLocaleString({'dateStyle':'long'}) //=> 5 octobre 2024
DateTime.setZone()
Description: 将 DateTime 转换为指定的时区。除非在选项中另行指定,DateTime 仍然表示相同的时刻。另请参阅 toLocal() 和 toUTC()。
Syntax: 语法: DateTime.setZone(区域,选项)
Returns: 返回: 日期时间
Type: 类型: 勒克森
Parameters: 参数:
zone(String) - 可选 - 时区标识符,格式为 'America/New_York'、'UTC+3',或字符串 'local' 或 'utc'opts(Object) - 可选 - 影响输出的选项。可用属性:keepCalendarTime(boolean):是否保持时间不变而仅更改偏移量。默认为 false。
Examples: 示例:
javascript
// dt = "2024-01-01T00:00:00.000+02:00".toDateTime()
dt.setZone('America/Buenos_aires') //=> 2023-12-31T19:00:00.000-03:00
javascript
// 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(单位,选项)
Returns: 返回: 日期时间
Type: 类型: 勒克森
Parameters: 参数:
unit(String) - 要取整到开始的单位。可选值:year、quarter、month、week、day、hour、minute、second或millisecond。opts(Object) - 可选 - 包含影响输出选项的对象。可用属性:useLocaleWeeks(boolean):是否在计算一周的开始时使用区域设置。默认为 false。
Examples: 示例:
javascript
// dt = "2024-03-20T18:49".toDateTime()
dt.startOf('month') //=> 2024-03-01T00:00
DateTime.toISO()
Description: 返回 DateTime 的 ISO 8601 标准字符串表示
Syntax: 语法: DateTime.toISO(可选)
Returns: 返回: 字符串
Type: 类型: 勒克森
Parameters: 参数:
opts(Object) - 可选 - 配置选项。详见 Luxon 文档。
Examples: 示例:
javascript
$now.toISO() //=> 2024-04-05T18:44:55.525+02:00
DateTime.toLocal()
Description: 将 DateTime 转换为工作流的本地时区。除非在参数中另行指定,DateTime 仍然表示相同的时刻。工作流的时区可以在工作流设置中更改。
Syntax: 语法: DateTime.toLocal()
Returns: 返回: 日期时间
Type: 类型: 勒克森
Examples: 示例:
javascript
// 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: 返回: 字符串
Type: 类型: 勒克森
Parameters: 参数:
formatOpts(Object) - 可选 - 渲染的配置选项。完整列表请参见 Intl.DateTimeFormat。默认渲染短日期格式。
Examples: 示例:
javascript
$now.toLocaleString() //=> '4/30/2024'
$now.toLocaleString({'dateStyle':'medium', 'timeStyle':'short'}) //=> 'Apr 30, 2024, 10:00 PM'
// (if in US English locale)
javascript
$now.setLocale('de-DE').toLocaleString() //=> '30.4.2024'
javascript
$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)
javascript
$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)
javascript
$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)
javascript
$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: 返回: 编号
Type: 类型: 勒克森
Examples: 示例:
javascript
$now.toMillis() //=> 1712334324677
DateTime.toRelative()
Description: 返回相对于当前时间的文本表示,例如 'in two days'。默认向下取整。
Syntax: 语法: DateTime.toRelative(选项)
Returns: 返回: 字符串
Type: 类型: 勒克森
Parameters: 参数:
options(Object) - 可选 - 影响输出的选项。可用属性:unit= 默认使用的单位(years、months、days等)。locale= 使用的语言和格式(例如de、fr)
Examples: 示例:
javascript
$now.plus(1, 'day').toRelative() //=> "in 1 day"
javascript
$now.plus(1, 'day').toRelative({unit:'hours'}) //=> "in 24 hours"
javascript
$now.plus(1, 'day').toRelative({locale:'es'}) //=> "dentro de 1 día"
DateTime.toSeconds()
Description: 返回以秒为单位的 Unix 时间戳(自 1970 年 1 月 1 日以来经过的秒数)
Syntax: 语法: DateTime.toSeconds()
Returns: 返回: 编号
Type: 类型: 勒克森
Examples: 示例:
javascript
$now.toSeconds() //=> 1712334442.372
DateTime.toString()
Description: 返回 DateTime 的字符串表示。类似于 toISO()。如需更多格式化选项,请参见 format() 或 toLocaleString()。
Syntax: 语法: DateTime.toString()
Returns: 返回: 字符串
Type: 类型: 勒克森
Examples: 示例:
javascript
$now.toString() //=> 2024-04-05T18:44:55.525+02:00
DateTime.toUTC()
Description: 将 DateTime 转换为 UTC 时区。除非在参数中另行指定,DateTime 仍然表示相同的时刻。使用 setZone() 可转换到其他时区。
Syntax: 语法: DateTime.toUTC(偏移量,选项)
Returns: 返回: 日期时间
Type: 类型: 勒克森
Parameters: 参数:
offset(Number) - 可选 - 相对于 UTC 的偏移量(以分钟为单位)opts(Object) - 可选 - 包含影响输出选项的对象。可用属性:keepCalendarTime(boolean):是否保持时间不变而仅更改偏移量。默认为 false。
Examples: 示例:
javascript
// 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.工作日
Returns: 返回: 编号
Type: 类型: 勒克森
Examples: 示例:
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.weekday //=> 6
DateTime.weekdayLong 工作日长
Description: 星期几的完整文本名称,例如 'Wednesday'。如果未指定区域设置,则默认使用系统区域设置。
Syntax: 语法: DateTime.weekdayLong
Returns: 返回: 字符串
Type: 类型: 勒克森
Examples: 示例:
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.weekdayLong //=> 'Saturday'
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.setLocale('de-DE').weekdayLong //=> 'Samstag'
DateTime.weekdayShort 工作日短
Description: 星期几的缩写文本名称,例如 'Wed'。如果未指定区域设置,则默认使用系统区域设置。
Syntax: 语法: DateTime.weekdayShort
Returns: 返回: 字符串
Type: 类型: 勒克森
Examples: 示例:
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.weekdayShort //=> 'Sat'
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.setLocale('fr-FR').weekdayShort //=> 'sam.'
DateTime.weekNumber 周数
Description: 一年中的第几周(1-52 左右)
Syntax: 语法: DateTime.weekNumber
Returns: 返回: 编号
Type: 类型: 勒克森
Examples: 示例:
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.weekNumber //=> 13
DateTime.year 年
Description: 年份
Syntax: 语法: DateTime.年
Returns: 返回: 编号
Type: 类型: 勒克森
Examples: 示例:
javascript
// dt = "2024-03-30T18:49".toDateTime()
dt.year //=> 2024
DateTime.zone 区域
Description: 与该 DateTime 关联的时区
Syntax: 语法: DateTime.zone
Returns: 返回: 对象
Type: 类型: 勒克森
Examples: 示例:
javascript
$now.zone //=> {"zoneName": "Europe/Berlin", "valid": true}