'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 1:[1]

Another way to get the exact format,that you mentioned in the quesion (ex: January 20, 2021) it is achievable by below script

%dw 2.0
output application/json
---
date: payload.message as DateTime {unit : "milliseconds"}
as String {format: 'MMMM dd,yyyy'}

this script will give you output as:

{
  "date": "January 01,2014"
}

Solution 2:[2]

Convert Unix Timestamp to Date Time in Mule 4 Input : { "time":1388620296020 } ---------output---------- { "date": "01-Jan-2014 11:51:36" }

How to write the script, You understand correct format Date Time. %dw 2.0 output application/json

{ date: payload.time as DateTime {unit: "milliseconds"} as String {format: 'dd-MMM-yyyy hh:mm:ss'} }

For more info referred link : https://help.mulesoft.com/s/question/0D52T00004tIkcf/how-to-convert-unix-timestamp-to-normal-human-readable-timestamp-using-dw-20

Thanks

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anurag Sharma
Solution 2 Siva ramaKrishna