Search code examples
mulemulesoftanypoint-studiomule4

Cannot Properly Transform Unix Timestamp to DateTime in Mule 4


So I am trying to convert a Unix Timestamp into a Human Readable date format (ex: January 20, 2021).

This is the response that I get from an API which gives the Unix timestamp

"time":1388620296020

And then I try to transform it using the Transform Message, my code looks like this

date: (object.properties.time as DateTime) as String {
        format: "MMMM dd, yyyy"
    },

But the output I get after I deploy it, goes like this

"date": "August 17, +45973"

I am not sure why is it happening.


Solution

  • Epoch can be directly converted to DateTime in Dataweave

    Try This

    date: (object.properties.time as DateTime {unit : "milliseconds"})

    output:

    "date": "2014-01-01T23:51:36.02Z"

    You can check the output by entering in the below link as well

    https://www.unixtimestamp.com/

    https://www.epochconverter.com/