Loading

Non Leap Year Date is Getting Corrected if it Exceeds the Days in the Month

Udgivelsesdato: Jul 28, 2025
Løsning

SYMPTOM

Dataweave is correcting the dates for the 29th day if not leap year and for the day of the month if not within allowed days for the month instead of throwing an error and taking the default value.
%dw 2.0
output application/json
---
payload.message as String as Date {"format":"MMddyyyy" } default null
for Feb month which is not leap year the input is:

{
"message": "02291947"
}

Output is:

"02281947"


CAUSE

February 29,1947 is not a leap year and the last day of the month is the 28th. Thus, DataWeave is converting an invalid date (between 1-31 which can be correct for other months ) to the nearest correct date. Thus, the conversion is expected behavior.

If you try to pass anything other than the range of Day1 to 31 like February 32,1947 then you will get the default value i.e. null.

So, it will give the result as null if we define any default value otherwise it will give the below type of error which is expected:
Cannot coerce String (02321947) to Date, caused by: Text '02321947' could not be parsed: Invalid value for DayOfMonth (valid values 1 - 28/31): 32

4| payload.message as String as Date {"format":"MMddyyyy" }
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Trace:
  at main::main (line: 4, column: 1)
So, it is working as expected.

SOLUTION

If you still need to validate Feb 29th as null for non-leap years then you can use the below workaround.
%dw 2.0
output application/json
fun validDate(d) = if (d == d as Date {format: "MMddyyyy"} as String {format: "MMddyyyy"} ) d else null
---
{
noLeap: validDate('02292019'),
valid: validDate('02282019'),
invalid: validDate('04312019')
}
 
Vidensartikelnummer

001122447

 
Indlæser
Salesforce Help | Article