使用 Luxon 处理日期和时间(Date and time with Luxon)#
Luxon 是一个 JavaScript 库,它可以使处理日期和时间变得更容易。有关如何使用 Luxon 的详细信息,请参阅 Luxon 的文档。
n8n 在节点之间传递日期时是以字符串形式,因此你需要对它们进行解析。Luxon 可以让这个过程更简单。
🌐 n8n passes dates between nodes as strings, so you need to parse them. Luxon makes this easier.
Python support
Luxon is a JavaScript library. The two convenience variables created by n8n are available when using Python in the Code node, but their functionality is limited:
- You can't perform Luxon operations on these variables. For example, there is no Python equivalent for
$today.minus(...). - The generic Luxon functionality, such as Convert date string to Luxon, isn't available for Python users.
n8n 中的日期和时间行为(Date and time behavior in n8n)#
请注意以下事项:
🌐 Be aware of the following:
- 在工作流程中,n8n 会在节点之间将日期和时间转换为字符串。在对来自其他节点的日期和时间进行运算时,请牢记这一点。
- 在 n8n 中,推荐使用 Luxon 的
DateTime()。使用原生 JavaScript 的Date()在某些 n8n 功能中无法正常工作。例如,它不支持 工作流特定时区。 - 使用原生 JavaScript,你可以用
new Date('2019-06-23')将字符串转换为日期。在 Luxon 中,你必须使用显式指定格式的函数,例如DateTime.fromISO('2019-06-23')或DateTime.fromFormat("23-06-2019", "dd-MM-yyyy")。
在 n8n 中设置时区(Setting the timezone in n8n)#
Luxon 使用 n8n 时区。该值可以是:
🌐 Luxon uses the n8n timezone. This value is either:
- 默认:
America/New York - 为你的 n8n 实例设置的自定义时区,通过
GENERIC_TIMEZONE环境变量进行设置。 - 单个工作流的自定义时区,在工作流设置中配置。
常见任务(Common tasks)#
本节提供了一些常见操作的示例。更多示例和详细指南,请参阅 Luxon 官方文档。
🌐 This section provides examples for some common operations. More examples, and detailed guidance, are available in Luxon's own documentation.
获取当前日期时间或日期(Get the current datetime or date)#
使用 $now 和 $today Luxon 对象 来获取当前时间或日期:
🌐 Use the $now and $today Luxon objects to get the current time or day:
now:一个包含当前时间戳的 Luxon 对象。等同于DateTime.now()。today:一个包含当前时间戳的 Luxon 对象,向下舍入到天。相当于DateTime.now().set({ hour: 0, minute: 0, second: 0, millisecond: 0 })。
请注意,这些变量在转换为字符串时可能会返回不同的时间格式:
🌐 Note that these variables can return different time formats when cast as a string:
=== “表达式(JavaScript)”
1 2 3 4 5 6 7 8 | |
=== “代码节点(JavaScript)”
1 2 3 4 5 6 7 8 | |
1 2 3 4 5 6 | |
n8n 提供了内置的便捷函数,以支持在表达式中对日期进行数据转换。更多信息请参阅 数据转换函数 | 日期。
🌐 n8n provides built-in convenience functions to support data transformation in expressions for dates. Refer to Data transformation functions | Dates for more information.
将 JavaScript 日期转换为 Luxon 格式(Convert JavaScript dates to Luxon)#
将原生 JavaScript 日期转换为 Luxon 日期:
🌐 To convert a native JavaScript date to a Luxon date:
- 在表达式中,使用
.toDateTime()方法。例如,{{ (new Date()).ToDateTime() }}。 - 在代码节点中,使用
DateTime.fromJSDate()。例如,let luxondate = DateTime.fromJSDate(new Date())。
将日期字符串转换为 Luxon 格式(Convert date string to Luxon)#
你可以将日期字符串和其他日期格式转换为 Luxon DateTime 对象。你可以从标准格式或任意字符串进行转换。
🌐 You can convert date strings and other date formats to a Luxon DateTime object. You can convert from standard formats and from arbitrary strings.
Luxon DateTime 与 JavaScript Date 的区别
使用原生 JavaScript,你可以用 new Date('2019-06-23') 将字符串转换为日期。在 Luxon 中,你必须使用显式指定格式的函数,例如 DateTime.fromISO('2019-06-23') 或 DateTime.fromFormat("23-06-2019", "dd-MM-yyyy")。
如果你有一个受支持的标准技术格式的日期:(If you have a date in a supported standard technical format:)#
大多数日期使用 fromISO()。这会从 ISO 8601 字符串创建一个 Luxon DateTime。例如:
🌐 Most dates use fromISO(). This creates a Luxon DateTime from an ISO 8601 string. For example:
=== “表达式(JavaScript)”
1 2 3 | |
=== “代码节点(JavaScript)”
1 2 3 | |
Luxon 的 API 文档中有关于 fromISO 的更多信息。
🌐 Luxon's API documentation has more information on fromISO.
Luxon 提供了处理各种格式转换的函数。有关详细信息,请参阅 Luxon 的《解析技术格式》指南 Parsing technical formats。
🌐 Luxon provides functions to handle conversions for a range of formats. Refer to Luxon's guide to Parsing technical formats for details.
如果你有一个非标准格式的字符串日期:(If you have a date as a string that doesn't use a standard format:)#
使用 Luxon 的 临时解析。为此,请使用 fromFormat() 函数,提供字符串和描述格式的一组 令牌。
🌐 Use Luxon's Ad-hoc parsing. To do this, use the fromFormat() function, providing the string and a set of tokens that describe the format.
例如,你有 n8n 的成立日期,2019 年 6 月 23 日,格式为 23-06-2019。你想将其转换为 Luxon 对象:
🌐 For example, you have n8n's founding date, 23rd June 2019, formatted as 23-06-2019. You want to turn this into a Luxon object:
=== “表达式(JavaScript)”
1 2 3 | |
=== “代码节点(JavaScript)”
1 2 3 | |
在使用临时解析时,请注意 Luxon 关于限制的警告。如果出现意外结果,请尝试他们的调试指南。
🌐 When using ad-hoc parsing, note Luxon's warning about Limitations. If you see unexpected results, try their Debugging guide.
获取自今天起 n 天后的数据(Get n days from today)#
获取今天之前或之后的天数。
🌐 Get a number of days before or after today.
=== “表达式(JavaScript)”
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
=== “代码节点(JavaScript)”
1 2 3 4 5 6 7 8 9 10 11 | |
更多详细信息和示例,请参阅:
🌐 For more detailed information and examples, refer to:
- Luxon 的数学指南
- 他们关于 DateTime plus 和 DateTime minus 的 API 文档
创建易于理解的日期(Create human-readable dates)#
在 从今天起获取 n 天 中,示例获取的是当前日期的七天前,并将其作为 [Object: "yyyy-mm-dd-T00:00:00.000+00:00"](用于表达式)或 yyyy-mm-dd-T00:00:00.000+00:00(在代码节点中)返回。为了让它更易读,你可以使用 Luxon 的格式化函数。
🌐 In Get n days from today, the example gets the date seven days before the current date, and returns it as [Object: "yyyy-mm-dd-T00:00:00.000+00:00"] (for expressions) or yyyy-mm-dd-T00:00:00.000+00:00 (in the Code node). To make this more readable, you can use Luxon's formatting functions.
例如,你希望包含日期的字段格式为 DD/MM/YYYY,这样在2019年6月23日,它会返回 23/06/2019。
🌐 For example, you want the field containing the date to be formatted as DD/MM/YYYY, so that on the 23rd June 2019, it returns 23/06/2019.
此表达式获取今天之前的七天日期,并将其转换为 DD/MM/YYYY 格式。
🌐 This expression gets the date seven days before today, and converts it to the DD/MM/YYYY format.
=== “表达式(JavaScript)”
1 2 3 | |
=== “代码节点(JavaScript)”
1 2 3 | |
你可以更改格式。例如:
🌐 You can alter the format. For example:
=== “表达式(JavaScript)”
1 2 3 4 5 | |
=== “代码节点(JavaScript)”
1 2 3 4 5 | |
有关更多信息,请参阅 Luxon 的指南 toLocaleString(面向人类的字符串)。
🌐 Refer to Luxon's guide on toLocaleString (strings for humans) for more information.
获取两个日期之间的时间间隔(Get the time between two dates)#
要获取两个日期之间的时间差,请使用 Luxon 的 diffs 功能。它会将一个日期减去另一个日期,并返回一个持续时间。
🌐 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:
=== “表达式(JavaScript)”
1 2 3 4 5 | |
=== “代码节点(JavaScript)”
1 2 3 4 5 | |
有关更多信息,请参阅 Luxon 的 Diffs。
🌐 Refer to Luxon's Diffs for more information.
一个更长的例子:离圣诞节还有多少天?(A longer example: How many days to Christmas?)#
此示例整合了多个 Luxon 功能,使用了 JMESPath,并进行了一些基本的字符串操作。
🌐 This example brings together several Luxon features, uses JMESPath, and does some basic string manipulation.
情景:你想要一个倒计时到12月25日。每天,它应该告诉你距离圣诞节还有多少天。你不想每年都去更新它——它需要能够在每一年自动运行。
🌐 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.
=== “表达式(JavaScript)”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
=== “代码节点(JavaScript)”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |