Add seconds to datetime in Python

Add seconds to datetime in Python

If you are looking for a method to add seconds to datetime in Python, keep reading our article. Today, we will give some methods and detailed explanations to carry out this topic. 

Ways to add seconds to datetime in Python

Using module datetime

The module datetime provides functions to work in real-time. To create a determined time, use the datetime() or time() function with following syntax:

datetime(year, month, day, hour, minute, second)

time(hour, minute, second)

The module also provides a function named timedelta() that performs operations to work with datetime’s units. The function includes some arguments to determine the unit that you want to make the change. To add seconds to datetime, you can directly add an exact number of seconds by passing them to the argument seconds. 

Syntax:

timedelta(seconds = number_of_seconds)

Code:

import datetime as dt
 
beforeTime = dt.datetime(2022,6,22,14,32,47) 
sec = 28 

# Add 28 seconds to the current time
afterTime = beforeTime + dt.timedelta(seconds=sec)
 
print("Time is:\n", beforeTime)
print("Time after adding seconds is:\n", afterTime)

Result:

Time is:
 2022-06-22 14:32:47
Time after adding seconds is:
 2022-06-22 14:33:15

Using Pandas Library

Another way to add seconds to datetime is using the function Timedelta() in Pandas library. Generally, this function is almost the same as the timedelta() above. However, this function does not support year and month units. 

Syntax:

Timedelta(value, unit)

Parameters:

  • value: number of units
  • unit: type of units

Code:

import datetime
import pandas as pd
 
beforeTime = dt.datetime(2022,6,22,14,32,47) 
sec = 28
afterTime = beforeTime + pd.Timedelta(sec, "s")
 
print("Time is:\n", beforeTime)
print("Time after adding seconds is:\n", afterTime)

Result:

Time is:
 2022-06-22 14:32:47
Time after adding seconds is:
 2022-06-22 14:33:15

You can create a determined time in Pandas by the function Timestamp() and get the same result.

beforeTime = pd.Timestamp(2022,6,22,14,32,47)

Creating a function to modify time manually

In this way, we will consider units of the class datetime as integer numbers. Remember that each unit has a limitation. For example, the unit second ranges from 0 to 59. If the result exceeds the limitation, we must increase the upper unit to ensure precision. 

Because we need to check the precision of the function, we will use the present units as the parameter of the function datetime().

Code:

import datetime as dt
 
# Function to add seconds manually
def addSecond(seconds):
    # Take present unit to build time
    now = dt.datetime.now()
    year = now.year
    month = now.month
    day = now.day
    hour = now.hour
    minute = now.minute
    sec = now.second
   
    sec+=seconds
    if(sec>59):
        minute+=sec//60
        sec = sec%60
        if(minute>59):
            hour+=minute//60
            minute = minute%60
            if(hour>23):
                day+=hour//24
                hour = hour%24
 
    myTime = dt.datetime(year, month, day, hour, minute, sec)
    print("Manual time:\n", myTime)
    
# Get the time right now
now = dt.datetime.now()
 
sec = 259200
 
# Time after adding a few seconds
newTime = now + dt.timedelta(seconds=sec)
 
print("Now is:\n",now)
print("Time Delta:\n", newTime)
addSecond(sec)

Result:

Now is:
 2022-10-08 16:25:23.471214
Time Delta:
 2022-10-11 16:25:23.471214
Manual time:
 2022-10-11 16:25:23

As you can see, the result from our function is almost the same as the result when using the function timedelta(). It means our function is correct. 

Because months have different days, we create a simple function to represent our idea.

Summary

Our article shows you how to add seconds to datetime in Python. We strongly recommend using the timedelta() function in the module datetime as the most effective way.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *