The Python Oracle

How do I create a datetime in Python from milliseconds?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping

--

Chapters
00:00 How Do I Create A Datetime In Python From Milliseconds?
00:25 Accepted Answer Score 384
00:36 Answer 2 Score 20
01:41 Answer 3 Score 13
01:50 Answer 4 Score 17
01:55 Thank you

--

Full question
https://stackoverflow.com/questions/7484...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #datetime

#avk47



ACCEPTED ANSWER

Score 384


Just convert it to timestamp

datetime.datetime.fromtimestamp(ms/1000.0)



ANSWER 2

Score 20


What about this? I presume it can be counted on to handle dates before 1970 and after 2038.

target_datetime_ms = 200000 # or whatever
base_datetime = datetime.datetime(1970, 1, 1)
delta = datetime.timedelta(0, 0, 0, target_datetime_ms)
target_datetime = base_datetime + delta

as mentioned in the Python standard lib:

fromtimestamp() may raise ValueError, if the timestamp is out of the range of values supported by the platform C localtime() or gmtime() functions. It’s common for this to be restricted to years in 1970 through 2038.

Very obviously, this can be done in one line:

target_dt = datetime(1970, 1, 1) + timedelta(milliseconds=target_dt_ms)

... not only was this obvious from my answer, but the 2015 comment by jfs is also highly misleading, because it calls the variable utc_time.

Er no: it's not "time", it's datetime, and it's most definitely NOT UTC. The datetime with which we're concerned here is a "timezone-naive" datetime, as opposed to a timezone-aware datetime. Therefore definitely NOT UTC.

Search on this if you're not familiar with the issue.




ANSWER 3

Score 17


import pandas as pd

Date_Time = pd.to_datetime(df.NameOfColumn, unit='ms')



ANSWER 4

Score 13


Bit heavy because of using pandas but works:

import pandas as pd
pd.to_datetime(msec_from_java, unit='ms').to_pydatetime()