Signup/Sign In

How to convert a datetime object to seconds?

In this article, we will learn to convert datetime object to seconds in Python. We will use some built-in modules available and some custom codes as well to see them working. Let's first have a quick look over what are dates in Python.

Date in Python

In Python, we can work on Date functions by importing a built-in module datetime available in Python. We have date objects to work with dates. This datetime module contains dates in the form of year, month, day, hour, minute, second, and microsecond. The datetime module has many methods to return information about the date object. It requires date, month, and year values to compute the function. Date and time functions are compared like mathematical expressions between various numbers.

Convert Datetime Object to Seconds

In Python, the date and time module provides various functions for the manipulation of dates. We can also convert a datetime object into seconds by applying mathematical operations. For this conversion, datetime module provides total_seconds method, calender method and timestamp method to convert a datetime object to seconds. 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 will need to convert it before you use it.

Example: The total_seconds() Method

1. datetime.strptime() formats the input timestamp into hh:mm:ss.

2. datetime.datetime() takes a year, month, day as arguments to create datetime object.

3. datetime.total_seconds() returns the total number of seconds.

The below example takes the time string into the acceptable format of hh:mm:ss. Python provides an operation on datetime to compute the difference between two dates. It finds the difference between the initial datetime object and the datetime object created from the time string. The value returned is a timedelta object from which we can use the function total_seconds() to get the value in seconds.

import datetime

time = "01:01:09"
date_time = datetime.datetime.strptime(time, "%H:%M:%S")
a_timedelta = date_time - datetime.datetime(1900, 1, 1)
seconds = a_timedelta.total_seconds()

print(seconds)


3669.0

Example: Use timestamp() method

Python 3 provides datetime.timestamp() method to easily convert the datetime object to seconds. This method will only be useful if you need the number of seconds from 1970-01-01 UTC. It returns a float value representing even fractions of a second. It is assumed that the datetime object represents the local time, i.e. it will be the number of seconds from the current time at your location to 1970-01-01 UTC.

from datetime import datetime

#get current date
dt = datetime.today()  
seconds = dt.timestamp()
print(seconds)


1613408089.609163

Example: Use calender.timegm() method

Python 3 provides a standard library called calendar which has calendar.timegm() method to easily convert the datetime object to seconds. This method converts a datetime to seconds since Epoch i.e. 1970-01-01 UTC. As the above timestamp() method returns a float value, this method strips off the fractions of a second.

from datetime import datetime

#get current date
dt = datetime.today()  
seconds = dt.timestamp()
print(seconds)


1613408988

Conclusion

In this article, we learned to convert a datetime object into seconds format by using datetime module. We discussed the working of datetime functions using different code snippets.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.