In Python, how do you convert a `datetime` object to seconds?

PythonPython 2.7

Python Problem Overview


Apologies for the simple question... I'm new to Python... I have searched around and nothing seems to be working.

I have a bunch of datetime objects and I want to calculate the number of seconds since a fixed time in the past for each one (for example since January 1, 1970).

import datetime
t = datetime.datetime(2009, 10, 21, 0, 0)

This seems to be only differentiating between dates that have different days:

t.toordinal()

Any help is much appreciated.

Python Solutions


Solution 1 - Python

For the special date of January 1, 1970 there are multiple options.

For any other starting date you need to get the difference between the two dates in seconds. Subtracting two dates gives a timedelta object, which as of Python 2.7 has a total_seconds() function.

>>> (t-datetime.datetime(1970,1,1)).total_seconds()
1256083200.0

The starting date is usually specified in UTC, so for proper results the datetime you feed into this formula should be in UTC as well. If your datetime isn't in UTC already, you'll need to convert it before you use it, or attach a tzinfo class that has the proper offset.

As noted in the comments, if you have a tzinfo attached to your datetime then you'll need one on the starting date as well or the subtraction will fail; for the example above I would add tzinfo=pytz.utc if using Python 2 or tzinfo=timezone.utc if using Python 3.

Solution 2 - Python

Starting from Python 3.3 this becomes super easy with the datetime.timestamp() method. This of course will only be useful if you need the number of seconds from 1970-01-01 UTC.

from datetime import datetime
dt = datetime.today()  # Get timezone naive now
seconds = dt.timestamp()

The return value will be a float representing even fractions of a second. If the datetime is timezone naive (as in the example above), it will be assumed that the datetime object represents the local time, i.e. It will be the number of seconds from current time at your location to 1970-01-01 UTC.

Solution 3 - Python

To get the Unix time (seconds since January 1, 1970):

>>> import datetime, time
>>> t = datetime.datetime(2011, 10, 21, 0, 0)
>>> time.mktime(t.timetuple())
1319148000.0

Solution 4 - Python

Maybe off-the-topic: to get UNIX/POSIX time from datetime and convert it back:

>>> import datetime, time
>>> dt = datetime.datetime(2011, 10, 21, 0, 0)
>>> s = time.mktime(dt.timetuple())
>>> s
1319148000.0

# and back
>>> datetime.datetime.fromtimestamp(s)
datetime.datetime(2011, 10, 21, 0, 0)

Note that different timezones have impact on results, e.g. my current TZ/DST returns:

>>>  time.mktime(datetime.datetime(1970, 1, 1, 0, 0).timetuple())
-3600 # -1h

therefore one should consider normalizing to UTC by using UTC versions of the functions.

Note that previous result can be used to calculate UTC offset of your current timezone. In this example this is +1h, i.e. UTC+0100.

References:

Solution 5 - Python

int (t.strftime("%s")) also works

Solution 6 - Python

from the python docs:

timedelta.total_seconds()

Return the total number of seconds contained in the duration. Equivalent to

(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6

computed with true division enabled.

Note that for very large time intervals (greater than 270 years on most platforms) this method will lose microsecond accuracy.

This functionality is new in version 2.7.

Solution 7 - Python

To convert a datetime object that represents time in UTC to POSIX timestamp:

from datetime import timezone

seconds_since_epoch = utc_time.replace(tzinfo=timezone.utc).timestamp()

To convert a datetime object that represents time in the local timezone to POSIX timestamp:

import tzlocal # $ pip install tzlocal

local_timezone = tzlocal.get_localzone()
seconds_since_epoch = local_timezone.localize(local_time, is_dst=None).timestamp()

See How do I convert local time to UTC in Python? If the tz database is available on a given platform; a stdlib-only solution may work.

Follow the links if you need solutions for <3.3 Python versions.

Solution 8 - Python

I tried the standard library's calendar.timegm and it works quite well:

# convert a datetime to milliseconds since Epoch
def datetime_to_utc_milliseconds(aDateTime):
    return int(calendar.timegm(aDateTime.timetuple())*1000)

Ref: <https://docs.python.org/2/library/calendar.html#calendar.timegm>

Solution 9 - Python

Python provides operation on datetime to compute the difference between two date. In your case that would be:

t - datetime.datetime(1970,1,1)

The value returned is a timedelta object from which you can use the member function total_seconds to get the value in seconds.

(t - datetime.datetime(1970,1,1)).total_seconds()

Solution 10 - Python

import datetime
import math


def getSeconds(inputDate):
    time = datetime.date.today().strftime('%m/%d/%Y')
    date_time = datetime.datetime.strptime(time, '%m/%d/%Y')
    msg = inputDate
    props = msg.split(".")
    a_timedelta = datetime.timedelta
    if(len(props)==3):
        a_timedelta = date_time - datetime.datetime(int(props[0]),int(props[1]),int(props[2]))
    else:
        print("Invalid date format")
        return
    seconds = math.trunc(a_timedelta.total_seconds())
    print(seconds)
    return seconds

Example getSeconds("2022.1.1")

Solution 11 - Python

I do not see this in all of the answers, although I guess it is the default need:

t_start = datetime.now()
sleep(2)
t_end = datetime.now()
duration = t_end - t_start
print(round(duration.total_seconds()))

If you do not use .total_seconds(), it throws: TypeError: type datetime.timedelta doesn't define __round__ method.

Example:

>>> duration
datetime.timedelta(seconds=53, microseconds=621861)
>>> round(duration.total_seconds())
54
>>> duration.seconds
53

Taking duration.seconds takes only the seconds, leaving aside the microseconds, the same as if you ran math.floor(duration.total_seconds()).

Solution 12 - Python

The standard way to find the processing time in ms of a block of code in python 3.x is the following:

import datetime

t_start = datetime.datetime.now()

# Here is the python3 code, you want 
# to check the processing time of

t_end = datetime.datetime.now()
print("Time taken : ", (t_end - t_start).total_seconds()*1000, " ms")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNathan LippiView Question on Stackoverflow
Solution 1 - PythonMark RansomView Answer on Stackoverflow
Solution 2 - PythonAndrzej PronobisView Answer on Stackoverflow
Solution 3 - PythonMark ByersView Answer on Stackoverflow
Solution 4 - PythonRobert LujoView Answer on Stackoverflow
Solution 5 - Pythondan3View Answer on Stackoverflow
Solution 6 - PythonMichaelView Answer on Stackoverflow
Solution 7 - PythonjfsView Answer on Stackoverflow
Solution 8 - PythonShaohong LiView Answer on Stackoverflow
Solution 9 - PythonYash ChitrodaView Answer on Stackoverflow
Solution 10 - PythonFaisal AminView Answer on Stackoverflow
Solution 11 - Pythonquestionto42standswithUkraineView Answer on Stackoverflow
Solution 12 - PythonSamay PashineView Answer on Stackoverflow