I am having a problem converting from ms to datetime. I get wrong datetime when printing.
I want to convert 1701092673 ms to datetime.
The result I am expecting is 2023-11-27 02:44:33
import datetime
ms = 1701092673
dt = datetime.datetime.fromtimestamp(ms/1000).strftime('%Y-%m-%d %H:%M:%S')
print (dt)
python prints 1970-01-20 17:31:32
I also tried the code below:
import datetime
ms = 1701092673
dt = datetime.datetime.fromtimestamp(ms).strftime('%Y-%m-%d %H:%M:%S')
print (dt)
Python prints 2023-11-27 14:44:33
Do you expect 12-hour time? Then change %H
to %I
:
import datetime
ms = 1701092673
dt = datetime.datetime.fromtimestamp(ms).strftime("%Y-%m-%d %I:%M:%S")
print(dt)
Prints:
2023-11-27 02:44:33
%I
- Hour (12-hour clock) as a zero-padded decimal number.%H
- Hour (24-hour clock) as a zero-padded decimal number.