Skip to content

使用 Luxon 处理日期和时间#

Luxon 是一个 JavaScript 库,使处理日期和时间变得更容易。有关如何使用 Luxon 的完整详细信息,请参阅 Luxon 的文档

n8n 在节点之间以字符串形式传递日期,因此您需要解析它们。Luxon 使这变得更容易。

Python 支持

Luxon 是一个 JavaScript 库。n8n 创建的两个便利变量在代码节点中使用 Python 时可用,但它们的功能有限:

  • 您不能对这些变量执行 Luxon 操作。例如,没有 Python 等效的 $today.minus(...)
  • 通用 Luxon 功能,如将日期字符串转换为 Luxon,对 Python 用户不可用。

变量#

n8n 使用 Luxon 提供两个自定义变量:

  • now:包含当前时间戳的 Luxon 对象。等效于 DateTime.now()
  • today:包含当前时间戳的 Luxon 对象,向下舍入到天。等效于 DateTime.now().set({ hour: 0, minute: 0, second: 0, millisecond: 0 })

请注意,这些变量在转换为字符串时可以返回不同的时间格式。这与 Luxon 的 DateTime.now() 行为相同。

1
2
3
4
5
6
{{$now}}
// n8n 显示 ISO 格式的时间戳
// 例如 2022-03-09T14:02:37.065+00:00
{{"Today's date is " + $now}}
// n8n 显示 "Today's date is <unix timestamp>"
// 例如 "Today's date is 1646834498755"
1
2
3
4
5
6
$now
// n8n 显示 <ISO 格式的时间戳>
// 例如 2022-03-09T14:00:25.058+00:00
let rightNow = "Today's date is " + $now
// n8n 显示 "Today's date is <unix timestamp>"
// 例如 "Today's date is 1646834498755"
1
2
3
4
5
6
_now
# n8n 显示 <ISO 格式的时间戳>
# 例如 2022-03-09T14:00:25.058+00:00
rightNow = "Today's date is " + str(_now)
# n8n 显示 "Today's date is <unix timestamp>"
# 例如 "Today's date is 1646834498755"

n8n 提供内置便利函数来支持表达式中日期的数据转换。有关更多信息,请参阅数据转换函数 | 日期

n8n 中的日期和时间行为#

请注意以下事项:

  • 在工作流中,n8n 在节点之间将日期和时间转换为字符串。在对来自其他节点的日期和时间进行算术运算时,请记住这一点。
  • 使用原生 JavaScript,您可以使用 new Date('2019-06-23') 将字符串转换为日期。在 Luxon 中,您必须使用明确说明格式的函数,如 DateTime.fromISO('2019-06-23')DateTime.fromFormat("23-06-2019", "dd-MM-yyyy")

在 n8n 中设置时区#

Luxon 使用 n8n 时区。此值是:

  • 默认:America/New York
  • 您的 n8n 实例的自定义时区,使用 GENERIC_TIMEZONE 环境变量设置。
  • 单个工作流的自定义时区,在工作流设置中配置。

常见任务#

本节提供了一些常见操作的示例。更多示例和详细指导可在 Luxon 自己的文档中找到。

将日期字符串转换为 Luxon#

您可以将日期字符串和其他日期格式转换为 Luxon DateTime 对象。您可以从标准格式和任意字符串转换。

Luxon DateTime 和 JavaScript Date 之间的区别

使用原生 JavaScript,您可以使用 new Date('2019-06-23') 将字符串转换为日期。在 Luxon 中,您必须使用明确说明格式的函数,如 DateTime.fromISO('2019-06-23')DateTime.fromFormat("23-06-2019", "dd-MM-yyyy")

如果您有支持的标准技术格式的日期:#

大多数日期使用 fromISO()。这从 ISO 8601 字符串创建 Luxon DateTime。例如:

1
{{DateTime.fromISO('2019-06-23T00:00:00.00')}}
1
let luxonDateTime = DateTime.fromISO('2019-06-23T00:00:00.00')

Luxon 的 API 文档有关于 fromISO 的更多信息。

Luxon 提供函数来处理一系列格式的转换。有关详细信息,请参阅 Luxon 的解析技术格式指南。

如果您有不使用标准格式的字符串日期:#

使用 Luxon 的临时解析。为此,请使用 fromFormat() 函数,提供字符串和一组描述格式的标记

例如,您有 n8n 的成立日期,2019 年 6 月 23 日,格式为 23-06-2019。您想将其转换为 Luxon 对象:

1
{{DateTime.fromFormat("23-06-2019", "dd-MM-yyyy")}}
1
let newFormat = DateTime.fromFormat("23-06-2019", "dd-MM-yyyy")

使用临时解析时,请注意 Luxon 关于限制的警告。如果您看到意外结果,请尝试他们的调试指南。

获取从今天开始的 n 天#

获取今天之前或之后的天数。

例如,您希望设置一个字段始终显示当前日期前七天的日期。

在表达式编辑器中,输入:

1
{{$today.minus({days: 7})}}

在 2019 年 6 月 23 日,这返回 [Object: "2019-06-16T00:00:00.000+00:00"]

此示例使用 n8n 的自定义变量 $today 以便于使用。它等效于 DateTime.now().set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).minus({days: 7})

例如,您希望一个变量包含当前日期前七天的日期。

在代码编辑器中,输入:

1
let sevenDaysAgo = $today.minus({days: 7})

在 2019 年 6 月 23 日,这返回 [Object: "2019-06-16T00:00:00.000+00:00"]

此示例使用 n8n 的自定义变量 $today 以便于使用。它等效于 DateTime.now().set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).minus({days: 7})

有关更详细的信息和示例,请参阅:

创建人类可读的日期#

获取从今天开始的 n 天中,示例获取当前日期前七天的日期,并将其返回为 [Object: "yyyy-mm-dd-T00:00:00.000+00:00"](对于表达式)或 yyyy-mm-dd-T00:00:00.000+00:00(在代码节点中)。为了使其更易读,您可以使用 Luxon 的格式化函数。

例如,您希望包含日期的字段格式化为 DD/MM/YYYY,以便在 2019 年 6 月 23 日,它返回 23/06/2019

此表达式获取今天前七天的日期,并将其转换为 DD/MM/YYYY 格式。

1
{{$today.minus({days: 7}).toLocaleString()}}
1
let readableSevenDaysAgo = $today.minus({days: 7}).toLocaleString()

您可以更改格式。例如:

1
{{$today.minus({days: 7}).toLocaleString({month: 'long', day: 'numeric', year: 'numeric'})}}

在 2019 年 6 月 23 日,这返回"2019 年 6 月 16 日"。

1
let readableSevenDaysAgo = $today.minus({days: 7}).toLocaleString({month: 'long', day: 'numeric', year: 'numeric'})

在 2019 年 6 月 23 日,这返回"2019 年 6 月 16 日"。

Refer to Luxon's guide on toLocaleString (strings for humans) for more information.

Get the time between two dates#

To get the time between two dates, use Luxon's diffs feature. This subtracts one date from another and returns a duration.

For example, get the number of months between two dates:

1
{{DateTime.fromISO('2019-06-23').diff(DateTime.fromISO('2019-05-23'), 'months').toObject()}}

This returns [Object: {"months":1}].

1
let monthsBetweenDates = DateTime.fromISO('2019-06-23').diff(DateTime.fromISO('2019-05-23'), 'months').toObject()

This returns {"months":1}.

Refer to Luxon's Diffs for more information.

A longer example: How many days to Christmas?#

This example brings together several Luxon features, uses JMESPath, and does some basic string manipulation.

The scenario: you want a countdown to 25th December. Every day, it should tell you the number of days remaining to Christmas. You don't want to update it for next year - it needs to seamlessly work for every year.

1
{{"There are " + $today.diff(DateTime.fromISO($today.year + '-12-25'), 'days').toObject().days.toString().substring(1) + " days to Christmas!"}}

This outputs "There are <number of days> days to Christmas!". For example, on 9th March, it outputs "There are 291 days to Christmas!".

A detailed explanation of what the expression does:

  • {{: indicates the start of the expression.
  • "There are ": a string.
  • +: used to join two strings.
  • $today.diff(): This is similar to the example in Get the time between two dates, but it uses n8n's custom $today variable.
  • DateTime.fromISO($today.year + '-12-25'), 'days': this part gets the current year using $today.year, turns it into an ISO string along with the month and date, and then takes the whole ISO string and converts it to a Luxon DateTime data structure. It also tells Luxon that you want the duration in days.
  • toObject() turns the result of diff() into a more usable object. At this point, the expression returns [Object: {"days":-<number-of-days>}]. For example, on 9th March, [Object: {"days":-291}].
  • .days uses JMESPath syntax to retrieve just the number of days from the object. For more information on using JMESPath with n8n, refer to our JMESpath documentation. This gives you the number of days to Christmas, as a negative number.
  • .toString().substring(1) turns the number into a string and removes the -.
  • + " days to Christmas!": another string, with a + to join it to the previous string.
  • }}: indicates the end of the expression.
1
let daysToChristmas = "There are " + $today.diff(DateTime.fromISO($today.year + '-12-25'), 'days').toObject().days.toString().substring(1) + " days to Christmas!";

This outputs "There are <number of days> days to Christmas!". For example, on 9th March, it outputs "There are 291 days to Christmas!".

A detailed explanation of what the code does:

  • "There are ": a string.
  • +: used to join two strings.
  • $today.diff(): This is similar to the example in Get the time between two dates, but it uses n8n's custom $today variable.
  • DateTime.fromISO($today.year + '-12-25'), 'days': this part gets the current year using $today.year, turns it into an ISO string along with the month and date, and then takes the whole ISO string and converts it to a Luxon DateTime data structure. It also tells Luxon that you want the duration in days.
  • toObject() turns the result of diff() into a more usable object. At this point, the expression returns [Object: {"days":-<number-of-days>}]. For example, on 9th March, [Object: {"days":-291}].
  • .days uses JMESPath syntax to retrieve just the number of days from the object. For more information on using JMESPath with n8n, refer to our JMESpath documentation. This gives you the number of days to Christmas, as a negative number.
  • .toString().substring(1) turns the number into a string and removes the -.
  • + " days to Christmas!": another string, with a + to join it to the previous string.