Doug Ayers
Doug Ayers

Reputation: 1078

DataWeave: How to create a date for months or days 1-9 using dw::core::Dates function?

Background

According to DataWeave documentation for the date(..) function, a Date can be created passing in the year, month, and day parts:

Input:

%dw 2.0
import * from dw::core::Dates
output application/json
---
{
   newDate: date({year: 2012, month: 10, day: 11})
}

Output:

{
   "newDate": "2012-10-11"
}

My Problem

However, for any month or day value 1 through 9 I get the following error:

dataweave date error

It seems that DataWeave is trying to create a date but is not left-padding the numbers with a 0 so that it's ISO8601 yyyy-MM-dd compliant.

I tried passing in a left-padded string 01 but I get the following error about the function expecting a number:

dataweave date error

Upvotes: 1

Views: 420

Answers (2)

Doug Ayers
Doug Ayers

Reputation: 1078

This issue (DW-894) is now resolved per the November 2021 Mule Runtime release notes:

Functions in the Dates module now accept one-digit numbers for months and days.

Upgrade AnypointStudio to patch release version: 4.4.0-20211026 or later.

Upvotes: 0

Doug Ayers
Doug Ayers

Reputation: 1078

As a workaround, I've created a custom function to left pad the values so that they match ISO8601 yyyy-MM-dd format. Not ideal, but it works.

%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' }
)

dataweave playground

Upvotes: 1

Related Questions