Javaexercise.com

How to Add Seconds To Date time In Python?

Python is a high-level, interpreted programming language, and is one of the most popular programming languages present in the software industry.

One of the most important concepts in python is that of adding seconds to a date-time in python.

Let us consider an example where we have an arbitrary starting date-time, say 30th of March in the year 2022, 11 hours and we need to add 2 seconds to this date-time.

The resulting date-time should be the 30th of March in the year 2022, 11 hours and 2 seconds.

Let us have a look at the different ways of doing this in detail.

Adding Seconds to date-time by using the datetime and timedelta libraries in Python

In this method, we use the datetime and timedelta libraries to add seconds to a datetime.

To work with these libraries we must first import them using the import statement.

We define a variable start_datetime that stores the starting datetime. The desired starting datetime in this example is the 30th of March in the year 2022, 11 hours.

This is specified using the datetime() function with the parameters year, month, day, and hours passed as 2022, 3, 30, and 11 respectively.

Next, we add 2 seconds to this starting date using the timedelta() function, with the parameter seconds passed as 2, and the addition operator +.

The result is stored in the variable new_datetime and later printed. 

Let us look at the Python 3 code and corresponding output for this method:

# Importing the required libraries
from datetime import datetime, timedelta

# Starting date
start_datetime = datetime(year = 2022, month = 3, day = 30, hour = 11)

# Performing the operation
new_datetime = start_datetime + timedelta(seconds=2)

# Printing
print(new_datetime)

Output

2022-03-30 11:00:02

Adding Seconds to the date-time by using the Pandas library

In this method, we use the Pandas library to add seconds to a datetime.

To work with these libraries we must first import them using the import statement.

We define a variable start_datetime that stores the starting datetime. The desired starting datetime in this example is 30th of March in the year 2022, 11 hours and we need to add 2 seconds to this.

This is specified using the pandas.to_datetime() function with the parameter passed as the date in string format in the form 30/3/2022 11:00:00, i.e. day month year hours minutes seconds format.

Next, we add 2 seconds to this starting datetime using the pandas.to_timedelta() function by passing the parameter as 2 and unit as s to specify that we are mentioning seconds. The result is stored in the variable new_datetime which is later printed. 

Let us look at the Python 3 code and corresponding output for this method:

# Importing the required libraries
import pandas as pd

# Starting datetime
start_datetime = pd.to_datetime('30/3/2022 11:00:00')

# Performing the operation
new_datetime = start_datetime + pd.to_timedelta(2, unit = 's')

# Printing
print(new_datetime)

Output

2022-03-30 11:00:02

Adding seconds to the date-time by using the relativedelta library in Python

In this method, we use the relativedelta library to add seconds to a datetime object.

To work with these libraries, we must first import them using the import statement. We define a variable start_datetime that stores the starting datetime.

The desired starting datetime in this example is the 30th of March in the year 2022, 11 hours.

This is specified using the datetime() function with the parameters year, month, day, and hours passed as 2022, 3, 30, and 11 respectively.

Next, we add 2 seconds to this starting date using the relativedelta() function by passing the parameter seconds as 2. The result is stored in the variable new_datetime which is later printed.

Let us look at the Python 3 code and corresponding output:

# Importing the required libraries
from datetime import datetime
from dateutil.relativedelta import relativedelta

# Starting datetime
start_datetime = datetime(year = 2022, month = 3, day = 30, hour = 11)

# Performing the operation
new_datetime = start_datetime + relativedelta(seconds=2)

# Printing
print(new_datetime)

Output

2022-03-30 11:00:02

Conclusion

In this topic, we have learned the use and advantages of adding seconds to a date-time in a Python program, following some simple running examples of programs, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.