DataWeave:如何使用 dw::core::Dates 函数创建月份或第 1-9 天的日期?
DataWeave: How to create a date for months or days 1-9 using dw::core::Dates function?
背景
根据 DataWeave documentation 对于 date(..)
函数,可以通过传入 year
、month
和 [=17= 创建一个 Date
] 零件:
输入:
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
newDate: date({year: 2012, month: 10, day: 11})
}
输出:
{
"newDate": "2012-10-11"
}
我的问题
但是,对于任何 month
或 day
值 1 到 9,我得到以下错误:
似乎 DataWeave 正在尝试创建一个日期,但没有用 0
左填充数字,以便它符合 ISO8601 yyyy-MM-dd
。
我尝试传递一个左填充字符串 01
但我收到以下有关需要数字的函数的错误:
作为解决方法,我创建了一个自定义函数来左填充值,以便它们匹配 ISO8601 yyyy-MM-dd
格式。不理想,但它有效。
%dw 2.0
import * from dw::core::Dates
import * from dw::core::Strings
fun toDate(options: {
year: Number,
month: Number,
day: Number
}): Date = (
(
[
leftPad(options.year as String, 4, '0'),
leftPad(options.month as String, 2, '0'),
leftPad(options.day as String, 2, '0'),
] joinBy ('-')
) as Date { format: 'yyyy-MM-dd' }
)
此问题 (DW-894
) 现已根据 2021 年 11 月 Mule Runtime release notes:
解决
Functions in the Dates
module now accept one-digit numbers for months and days.
将AnypointStudio升级到补丁发布版本:4.4.0-20211026或更高版本。
背景
根据 DataWeave documentation 对于 date(..)
函数,可以通过传入 year
、month
和 [=17= 创建一个 Date
] 零件:
输入:
%dw 2.0
import * from dw::core::Dates
output application/json
---
{
newDate: date({year: 2012, month: 10, day: 11})
}
输出:
{
"newDate": "2012-10-11"
}
我的问题
但是,对于任何 month
或 day
值 1 到 9,我得到以下错误:
似乎 DataWeave 正在尝试创建一个日期,但没有用 0
左填充数字,以便它符合 ISO8601 yyyy-MM-dd
。
我尝试传递一个左填充字符串 01
但我收到以下有关需要数字的函数的错误:
作为解决方法,我创建了一个自定义函数来左填充值,以便它们匹配 ISO8601 yyyy-MM-dd
格式。不理想,但它有效。
%dw 2.0
import * from dw::core::Dates
import * from dw::core::Strings
fun toDate(options: {
year: Number,
month: Number,
day: Number
}): Date = (
(
[
leftPad(options.year as String, 4, '0'),
leftPad(options.month as String, 2, '0'),
leftPad(options.day as String, 2, '0'),
] joinBy ('-')
) as Date { format: 'yyyy-MM-dd' }
)
此问题 (DW-894
) 现已根据 2021 年 11 月 Mule Runtime release notes:
Functions in the
Dates
module now accept one-digit numbers for months and days.
将AnypointStudio升级到补丁发布版本:4.4.0-20211026或更高版本。