0

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.

Kallol
  • 2,089
  • 3
  • 18
  • 33
  • 1
    Possible duplicate of [Format a datetime into a string with milliseconds](https://stackoverflow.com/questions/7588511/format-a-datetime-into-a-string-with-milliseconds) – luis.parravicini Oct 16 '19 at 10:01
  • @luis.parravicini The questions you referred the time which needs to convert is in datetime.datetime(2019, 10, 16, 10, 6, 30, 665979) format. Not in epoch format what I am looking for. So please remove the duplicate tag. – Kallol Oct 16 '19 at 10:08

1 Answers1

1

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