Search code examples
muledataweavemule4

How to handle abbreviated year pattern (yy) in Mule 4


Input:

{
  "date": "01-JAN-70"
}

Expected Output:

{
  "date": "1970-01-01"
}

appreciate your input


Solution

  • This is an answer that doesn't use string operations, which is not a good practice for dates handling. The function replaceCentury() let's you replace the century of a date with another one.

    %dw 2.0
    output application/json
    import * from dw::core::Dates
    fun centuryFromYear(c)=floor( (c / 100)) * 100
    // replaceCentury(): d is the input date, c is the century that you want the date to be in
    fun replaceCentury(d: Date, c: Number)= date({year: d.year - centuryFromYear(d.year) + c, month: d.month, day: d.day})
    var inputDate=payload.date as Date {format: "dd-MMM-yy"} 
    ---
    replaceCentury(inputDate, 1900)
    

    Output:

    "1970-01-01"