Search code examples
datetimedataweavemulesoft

Mulesoft Dataweave zero zoned timestamp conversion


I am looking to convert a Datetime field into ZonedDatetime but with Zero timestamp. I have a function like below -

fun dateInSecToDateTimePST(d : String) = if (d != null)
                            (d as Number as DateTime {unit: "seconds"} >> "CST")
                        else null

The output of this is

2020-11-20T21:30:00-06:00

But The output what I am expecting is like below-

2020-11-20T00:00:00-06:00

Timestamp will always remain zero but with offset. Any suggestion ? I am using %dw 2.0 and Mule4


Solution

  • With the date time types you can breakup the datetime input and concat it back together with the zeroed time piece like this:

    %dw 2.0
    output application/json
    fun dateInSecToDateTimePST(d : String) = do { 
        var date = (d as Number as DateTime {unit: "seconds"}) as Date
        var time = "00:00:00" as Time
        var zone = "CST" as TimeZone
        ---
        if (d != null)
            date ++ time ++ zone
        else null
    }
    ---
    dateInSecToDateTimePST(payload.time)
    

    you could also have the timezone be dynamic as well if needed.