Just convert it to timestamp

datetime.datetime.fromtimestamp(ms/1000.0)
Answer from vartec on Stack Overflow
Discussions

python - How to convert milliseconds to date and time format? - Stack Overflow
I have a value in milliseconds in a Python program. For example: 1557975599999 · And I would like to convert it to a string with days and hours, minutes, seconds. How can I do this? ... Is it correct to assume that this value is a "unix timestamp" - time since the epoch? Is it actually in microseconds, not in milliseconds? ... To convert unix timestamp to datetime... More on stackoverflow.com
🌐 stackoverflow.com
Convert python datetime to timestamp in milliseconds - Stack Overflow
How do I convert a human-readable time such as 20.12.2016 09:38:42,76 to a Unix timestamp in milliseconds? More on stackoverflow.com
🌐 stackoverflow.com
How to convert a number of milliseconds since Year 0 into a pandas Timestamp (or some other object with year, month, day, hour, minute, second)?
my_dataframe.timestamp_column = pandas.to_datetime(my_dataframe.timestamp_column) That should automatically detect the input format (unix timestamp; the number of milli/seconds since 1st Jan 1970) and convert that column to a pandas.DateTime type. If the dates are still off then you can pass the exact unit to the method ​ my_dataframe.timestamp_column = pandas.to_datetime(my_dataframe.timestamp_column, unit="ms") If you are working with a non-unix epoch (practically this just means the start of a range of time) you can pass the start date with the origin argument (starting at the new millenium for example): ​my_dataframe.timestamp_column = pandas.to_datetime(my_dataframe.timestamp_column, unit="ms", origin=pd.Timestamp("2000-01-01")) More on reddit.com
🌐 r/learnpython
10
11
October 25, 2022
python - Converting epoch time with milliseconds to datetime - Stack Overflow
I have used a ruby script to convert iso time stamp to epoch, the files that I am parsing has following time stamp structure: 2009-03-08T00:27:31.807 Since I want to keep milliseconds I used foll... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
gist.github.com › evnm › d17336bf42e887c6e756
Script to convert milliseconds since epoch to a human-readable timestamp · GitHub
Script to convert milliseconds since epoch to a human-readable timestamp · Raw · gistfile1.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
Python documentation
docs.python.org › 3 › library › datetime.html
datetime — Basic date and time types
days=50, ... seconds=27, ... microseconds=10, ... milliseconds=29000, ... minutes=5, ... hours=8, ... weeks=2 ... ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10) ... import datetime as dt instead of import datetime or from datetime import datetime to avoid confusion between the module and the class.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-use-strptime-with-milliseconds-in-python
How to use strptime with milliseconds in Python - GeeksforGeeks
July 23, 2025 - Here's how to do it: ... from datetime ... = datetime.strptime(s, format) print(dt_obj) ... Use %f when you want to include milliseconds or microseconds in your datetime string....
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › python-add-milliseconds-to-datetime
How to add Milliseconds to Datetime in Python | bobbyhadz
April 8, 2024 - The timedelta class can be passed a milliseconds argument and adds the specified number of milliseconds to the datetime object. ... Copied!from datetime import datetime, timedelta d = '2023-11-24 09:30:00.000123' # 👇️ Convert a string to ...
🌐
Reddit
reddit.com › r/learnpython › how to convert a number of milliseconds since year 0 into a pandas timestamp (or some other object with year, month, day, hour, minute, second)?
r/learnpython on Reddit: How to convert a number of milliseconds since Year 0 into a pandas Timestamp (or some other object with year, month, day, hour, minute, second)?
October 25, 2022 -

How to convert a number of milliseconds since Year 0 (from a CDF) into a pandas Timestamp (or some other object with year, month, day, hour, minute, second)?

I think I can get it to work, but the way I'm doing it is extremely convoluted, and i feel like there has to be a simpler way.

Edit: in case anyone happens to have the same problem as me, which is: converting an epoch from cdflib into a date and time, where it starts out as a number of milliseconds since year 0, all you have to do is:

import cdflib

cdf_file = cdflib.CDF("filename") 
epoch = cdf_file.varget("Epoch")  
time_array = cdflib.epochs.CDFepoch.to_datetime(epoch) 

This gives you information on the date and time for each point.

Here is the documentation:

https://cdflib.readthedocs.io/en/latest/api/cdflib.epochs.CDFepoch.html#cdflib.epochs.CDFepoch

🌐
InfluxData
influxdata.com › home › how to convert timestamp to datetime in python | influxdata
How to Convert Timestamp to DateTime in Python | InfluxData
June 28, 2023 - Python's fromtimestamp() expects a value in seconds, so if your timestamp is in milliseconds (common in JavaScript APIs and many databases), divide it by 1000 before passing it in: datetime.fromtimestamp(timestamp_ms / 1000).
🌐
Quora
quora.com › How-can-you-convert-milliseconds-in-a-format-with-days-hours-minutes-and-seconds-in-Python
How to convert milliseconds in a format with days, hours, minutes and seconds in Python - Quora
Answer (1 of 2): The easiest way is to use simple maths : [code]def convert_from_ms( milliseconds ): seconds, milliseconds = divmod(milliseconds,1000) minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) days, hours = ...
Top answer
1 of 2
7

The reason for this is that date_to_mil works with UTC and mil_to_date doesn't. You should replace utcfromtimestamp with fromtimestamp.

Further explanation:

In your code, epoch is the date of the epoch in UTC (but the object is without any time-zone). But date is local since fromtimestamp returns a local time:

If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive

So you subtract the UTC epoch from the local datetime and you get a delay which is your local delay to UTC.

2 of 2
4

If input is UTC then to get POSIX timestamp as integer milliseconds:

from datetime import datetime, timedelta

def timestamp_millis(utc_time, epoch=datetime(1970, 1, 1)):
    """Return milliseconds since Epoch as integer."""
    td = utc_time - epoch
    return (td.microseconds + (td.seconds + td.days * 86400) * 10**6) // 10**3

Note: the formula may produce a different result from: int(td.total_seconds() * 1000).

And in reverse: to get UTC time from POSIX time given as milliseconds:

def datetime_from_millis(millis, epoch=datetime(1970, 1, 1)):
    """Return UTC time that corresponds to milliseconds since Epoch."""
    return epoch + timedelta(milliseconds=millis)

It supports both positive and negative millis.

Note: None handling happens outside these functions.

Example:

>>> datetime_from_millis(1394462888000)
datetime.datetime(2014, 3, 10, 14, 48, 8)
>>> datetime.utcfromtimestamp(1394462888)
datetime.datetime(2014, 3, 10, 14, 48, 8)
>>> timestamp_millis(_)
1394462888000

The result is different from the one in your question!

gmtime(0).year != 1970 and TZ=right/UTC cases are ignored.

🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-parse-a-time-string-containing-milliseconds-in-python
How to Parse a Time String Containing Milliseconds in Python? - GeeksforGeeks
July 23, 2025 - Let's say we have a time string in the format YYYY-MM-DD HH:MM:SS.sss where sss represents milliseconds. Here, we define the time_string containing the date and time with milliseconds. The time_format specifies the format of the time string using datetime format codes. Then datetime.strptime() converts the string into a datetime object.
🌐
GitHub
gist.github.com › la5942 › 91aa8151cae6ab4164d2
Convert milliseconds to timedelta object (duration) · GitHub
Use time deltas to convert milliseconds into duration. http://docs.python.org/2/library/datetime.html#datetime.timedelta · from datetime import timedelta # Convert 600000ms (10 minutes) back to duration value. milliseconds = timedelta(mill...
🌐
Delft Stack
delftstack.com › home › howto › python › python datetime milliseconds
How to Convert DateTime to String With Milliseconds in Python | Delft Stack
February 2, 2024 - Python provides a straightforward way to achieve this using the strftime method. from datetime import datetime # Get current date and time current_datetime = datetime.now() # Convert to string with milliseconds formatted_datetime = ...
🌐
Hevodata
docs.hevodata.com › pipelines › transformations › python-transfm › timeutils › convert-epoch-to-datetime
Convert Epoch Time to a Datetime - Hevo Data
The following script takes an epoch value as an argument and returns a datetime object. from io.hevo.api import Event from io.hevo.api import TimeUtils def transform(event): # Get properties from the Event # <epoch> = <milliseconds_value> <date_object> = TimeUtils.fromEpochToDatetime(<epoch>) return <newEvent>
🌐
Statistics Globe
statisticsglobe.com › home › python programming language for statistics & data science › convert datetime into string with milliseconds in python (3 examples)
Convert datetime into String with Milliseconds in Python (3 Examples)
January 26, 2023 - Convert datetime Object to Date Only String in Python · Convert datetime to String without Microsecond Component ... Summary: This tutorial demonstrated how to transform a datetime object into a string with a milliseconds component in the Python programming language.
🌐
TutorialsPoint
tutorialspoint.com › how-to-use-strptime-with-milliseconds-in-python
How to use strptime with milliseconds in Python
October 13, 2023 - The following example shows the usage of the Python strptime() method. import time struct_time = time.strptime("30 Nov 00", "%d %b %y") print("Returned object:", struct_time) When we run above program, it produces following result − · Returned ...