使用 Luxon 处理日期和时间#
¥Date and time with Luxon
Luxon 是一个 JavaScript 库,可以更轻松地处理日期和时间。有关如何使用 Luxon 的完整详细信息,请参阅 Luxon 文档。
¥Luxon is a JavaScript library that makes it easier to work with date and time. For full details of how to use Luxon, refer to Luxon's documentation.
n8n 将日期以字符串形式在节点间传递,因此你需要对其进行解析。Luxon 让这一切变得更简单。
¥n8n passes dates between nodes as strings, so you need to parse them. Luxon makes this easier.
Python support
Luxon 是一个 JavaScript 库。n8n 创建的两个便捷 variables 可在代码节点中使用 Python 时使用,但其功能有限:
¥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:
- 无法对这些变量执行 Luxon 操作。例如,Python 中没有与
$today.minus(...)等效的属性。
¥You can't perform Luxon operations on these variables. For example, there is no Python equivalent for $today.minus(...).
- 通用的 Luxon 功能(例如 将日期字符串转换为 Luxon 格式)不适用于 Python 用户。
¥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 将节点之间的日期和时间转换为字符串。在对来自其他节点的日期和时间进行运算时,请记住这一点。
¥In a workflow, n8n converts dates and times to strings between nodes. Keep this in mind when doing arithmetic on dates and times from other nodes.
- 使用原生 JavaScript,你可以使用
new Date('2019-06-23')将字符串转换为日期。在 Luxon 中,你必须使用明确指定格式的函数,例如DateTime.fromISO('2019-06-23')或DateTime.fromFormat("23-06-2019", "dd-MM-yyyy")。
¥With vanilla JavaScript, you can convert a string to a date with new Date('2019-06-23'). In Luxon, you must use a function explicitly stating the format, such as DateTime.fromISO('2019-06-23') or 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
¥Default: America/New York
- n8n 实例的自定义时区,使用
GENERIC_TIMEZONE环境变量设置。
¥A custom timezone for your n8n instance, set using the GENERIC_TIMEZONE environment variable.
- 单个工作流的自定义时区,在工作流设置中配置。
¥A custom timezone for an individual workflow, configured in workflow settings.
常见任务#
¥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()。
¥now: a Luxon object containing the current timestamp. Equivalent to DateTime.now().
today:包含当前时间戳(向下取整到天)的 Luxon 对象。等效于DateTime.now().set({ hour: 0, minute: 0, second: 0, millisecond: 0 })。
¥today: a Luxon object containing the current timestamp, rounded down to the day. Equivalent to 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:
1 2 3 4 5 6 | |
1 2 3 4 5 6 | |
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() }}。
¥In expressions, use the .toDateTime() method. For example, {{ (new Date()).ToDateTime() }}.
- 在“代码”节点中,使用
DateTime.fromJSDate()。例如,let luxondate = DateTime.fromJSDate(new Date())。
¥In the Code node, use DateTime.fromJSDate(). For example, 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.
A difference between Luxon DateTime and JavaScript Date
使用原生 JavaScript,你可以使用 new Date('2019-06-23') 将字符串转换为日期。在 Luxon 中,你必须使用明确指定格式的函数,例如 DateTime.fromISO('2019-06-23') 或 DateTime.fromFormat("23-06-2019", "dd-MM-yyyy")。
¥With vanilla JavaScript, you can convert a string to a date with new Date('2019-06-23'). In Luxon, you must use a function explicitly stating the format, such as DateTime.fromISO('2019-06-23') or 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:
1 2 3 | |
1 2 3 | |
Luxon 的 API 文档包含有关 fromISO 的更多信息。
¥Luxon's API documentation has more information on fromISO.
Luxon 提供用于处理各种格式转换的函数。详情请参阅 Luxon 的 解析技术格式 指南。
¥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() 函数,并提供字符串和一组描述格式的 tokens。
¥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:
1 2 3 | |
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.
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 10 11 | |
更多详细信息和示例,请参阅:
¥For more detailed information and examples, refer to:
- Luxon 的 数学指南
¥Luxon's guide to math
¥Their API documentation on DateTime plus and DateTime minus
创建易于理解的日期#
¥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.
1 2 3 | |
1 2 3 | |
你可以更改格式。例如:
¥You can alter the format. For example:
1 2 3 4 5 | |
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:
1 2 3 4 5 | |
1 2 3 4 5 | |
有关更多信息,请参阅 Luxon 的 差异 文档。
¥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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |