Java Student
Java Student

Reputation: 19

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.

Upvotes: 0

Views: 1295

Answers (3)

Anurag Sharma
Anurag Sharma

Reputation: 935

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"
}

Upvotes: 0

Harsha Shivamurthy
Harsha Shivamurthy

Reputation: 467

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/

Upvotes: 2

Siva ramaKrishna
Siva ramaKrishna

Reputation: 1

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

Upvotes: -1

Related Questions