使用 Luxon 处理日期和时间#
Luxon 是一个 JavaScript 库,使处理日期和时间变得更容易。有关如何使用 Luxon 的完整详细信息,请参阅 Luxon 的文档。
n8n 在节点之间以字符串形式传递日期,因此您需要解析它们。Luxon 使这变得更容易。
Python 支持
Luxon 是一个 JavaScript 库。n8n 创建的两个便利变量在 Code 节点中使用 Python 时可用,但它们的功能有限:
- 您不能对这些变量执行 Luxon 操作。例如,没有 Python 等效的
$today.minus(...)。 - 通用 Luxon 功能,如将日期字符串转换为 Luxon,对 Python 用户不可用。
n8n 中的日期和时间行为#
请注意以下几点:
- 在工作流中,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 中设置时区#
Luxon 使用 n8n 时区。此值是:
- 默认:
America/New York - 您的 n8n 实例的自定义时区,使用
GENERIC_TIMEZONE环境变量设置。 - 单个工作流的自定义时区,在工作流设置中配置。
常见任务#
本节提供了一些常见操作的示例。更多示例和详细指导,请参阅 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 })。
请注意,这些变量在转换为字符串时可以返回不同的时间格式:
1 2 3 4 5 6 | |
1 2 3 4 5 6 | |
1 2 3 4 5 6 | |
n8n provides built-in convenience functions to support data transformation in expressions for dates. Refer to Expression reference for more information.
将 JavaScript 日期转换为 Luxon#
要将原生 JavaScript 日期转换为 Luxon 日期:
- In expressions, use the
.toDateTime()method. For example,{{ (new Date()).toDateTime() }}. - In the Code node, use
DateTime.fromJSDate(). For example,let luxondate = DateTime.fromJSDate(new Date()).
将日期字符串转换为 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 | |
1 | |
Luxon 的 API 文档有关于 fromISO 的更多信息。
Luxon 提供函数来处理一系列格式的转换。有关详细信息,请参阅 Luxon 的解析技术格式指南。
如果您有不使用标准格式的字符串日期:#
使用 Luxon 的临时解析。为此,请使用 fromFormat() 函数,提供字符串和一组描述格式的标记。
例如,您有 n8n 的成立日期,2019 年 6 月 23 日,格式为 23-06-2019。您想将其转换为 Luxon 对象:
1 | |
1 | |
使用临时解析时,请注意 Luxon 关于限制的警告。如果您看到意外结果,请尝试他们的调试指南。
获取从今天开始的 n 天#
获取今天之前或之后的天数。
例如,您希望设置一个字段始终显示当前日期前七天的日期。
在表达式编辑器中,输入:
1 | |
在 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 | |
在 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})。
有关更详细的信息和示例,请参阅:
- Luxon 的数学指南
- 他们关于 DateTime plus 和 DateTime minus 的 API 文档
创建人类可读的日期#
在获取从今天开始的 n 天中,示例获取当前日期前七天的日期,并将其返回为 [Object: "yyyy-mm-dd-T00:00:00.000+00:00"](对于表达式)或 yyyy-mm-dd-T00:00:00.000+00:00(在 Code 节点中)。为了使其更易读,您可以使用 Luxon 的格式化函数。
例如,您希望包含日期的字段格式化为 DD/MM/YYYY,以便在 2019 年 6 月 23 日,它返回 23/06/2019。
此表达式获取今天前七天的日期,并将其转换为 DD/MM/YYYY 格式。
1 | |
1 | |
您可以更改格式。例如:
1 | |
在 2019 年 6 月 23 日,这返回 "16 June 2019"。
1 | |
在 2019 年 6 月 23 日,这返回 "16 June 2019"。
有关更多信息,请参阅 Luxon 关于 toLocaleString(人类可读字符串) 的指南。
获取两个日期之间的时间#
要获取两个日期之间的时间,请使用 Luxon 的 diffs 功能。这从一个日期中减去另一个日期并返回一个持续时间。
例如,获取两个日期之间的月数:
1 | |
这返回 [Object: {"months":1}]。
1 | |
这返回 {"months":1}。
有关更多信息,请参阅 Luxon 的 Diffs。
一个更长的示例:距离圣诞节还有多少天?#
这个示例结合了多个 Luxon 功能,使用 JMESPath,并进行一些基本的字符串操作。
场景:您想要一个到 12 月 25 日的倒计时。每天,它应该告诉您距离圣诞节还有多少天。您不想为明年更新它 - 它需要无缝地适用于每一年。
1 | |
这输出 "There are
对表达式所做工作的详细解释:
{{: 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$todayvariable.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}]..daysuses 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 | |
这输出 "There are
对代码所做工作的详细解释:
"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$todayvariable.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}]..daysuses 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.