Padmaja Kattamuri
Padmaja Kattamuri

Reputation: 41

Convert number to datetime format

How do I Convert "1561994754" number to "2019-07-01T15:25:54.000000"

I have used :

import datetime    
datetime.datetime.fromtimestamp(x['date'] / 1000.0).strftime('%Y-%m-%d %H:%M:%S.%f')

But I am getting 1970-01-18 19:53:14.754000, can you please guide me to correct function?

Thanks, Aditya

Upvotes: 1

Views: 654

Answers (2)

hjobrien
hjobrien

Reputation: 141

Removing the / 1000 gives me '2019-07-01 08:25:54.000000', It seems like there was a unit mismatch in your expression. To exactly match the format you're asking for, datetime.datetime.fromtimestamp(x['date'], tz=datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.%f') produces '2019-07-01T15:25:54.000000 (leaving the timezone argument blank defaults to using local time, but the human-readable date in your question uses UTC)

Upvotes: 1

Deb Das
Deb Das

Reputation: 294

You can try like this!

String myString = String.valueOf(1561994754);
DateFormat format = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ssZ");
Date date = format.parse(myString);

Upvotes: 0

Related Questions