Kallol
Kallol

Reputation: 2189

How to get the millisecond part while converting to date time from epoch using python

I have a unix epoc time as 1571205166751

I want to convert it to date time in millisecond

The desired result should be 2019-10-16 05:52:46:751 AM

But using following code I am getting the result as 2019-10-16 05:52:46, not the millisecond part.

import time
time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(1571205166))

How to get the millisecond part while converting ?

P.S. I want to covert epoch time, not any other format.

Upvotes: 0

Views: 307

Answers (1)

Rushikesh Raut
Rushikesh Raut

Reputation: 198

Use datetime.datetime.fromtimestamp

epoch_time = 1571205166751

dt = datetime.datetime.fromtimestamp(epoch_time/1000)

dt.strftime("%Y-%m-%d %H:%M:%S.%f %p")

output:

'2019-10-16 11:22:46.751000 AM'

Note:

%f is not supported in time.strftime

Upvotes: 1

Related Questions