• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Add Seconds to Datetime Variable Using Python timedelta() Function

Add Seconds to Datetime Variable Using Python timedelta() Function

February 9, 2022 Leave a Comment

To add seconds to a datetime using Python, the easiest way is to use the Python timedelta() function from the datetime module.

from datetime import timedelta, datetime

now = datetime.now()

one_second_in_future = now + timedelta(seconds=1)
sixty_seconds_in_future = now + timedelta(seconds=60)
one_hour_in_future = now + timedelta(seconds=3600)

print(now)
print(one_second_in_future)
print(sixty_seconds_in_future)
print(one_hour_in_future)

#Output:
2022-02-09 15:45:53.655282
2022-02-09 15:45:54.655282
2022-02-09 15:46:53.655282
2022-02-09 16:45:53.655282

When working with data in Python, many times we are working with datetimes. Being able to manipulate and change dates easily is very important for efficient processing.

One such change is to be able to add seconds to a datetime variable.

With Python, we can easily add seconds to a date with the help of the datetime module.

To add seconds to a date in Python, we can use the timedelta() function from the datetime module.

Below is code that shows you how to add seconds to datetimes variables using Python.

from datetime import timedelta, datetime

now = datetime.now()

one_second_in_future = now + timedelta(seconds=1)
sixty_seconds_in_future = now + timedelta(seconds=60)
one_hour_in_future = now + timedelta(seconds=3600)

print(now)
print(one_second_in_future)
print(sixty_seconds_in_future)
print(one_hour_in_future)

#Output:
2022-02-09 15:45:53.655282
2022-02-09 15:45:54.655282
2022-02-09 15:46:53.655282
2022-02-09 16:45:53.655282

Note, here we need to use datetime.now(), and we can’t use date.today() because date won’t have the time associated with it.

If you want to subtract seconds, you can just subtract the call to the timedelta() function.

How to Add Seconds to a Date Variable With pandas in Python

If you are using the Python pandas module, we can add seconds to a datetime easily.

With pandas, to add seconds to a datetime, we use the DateOffset() function.

Below is an example of how to use pandas to add seconds to a datetime in Python.

import pandas as pd

startdate = "02/09/2022"
enddate = pd.to_datetime(startdate) + pd.DateOffset(seconds=60)

print(startdate)
print(enddate)

#Output:
2022-02-09 00:00:00
2022-02-09 00:01:00

Hopefully this article has been beneficial for you to learn how to add seconds to a datetime variable in Python.

Other Articles You'll Also Like:

  • 1.  Get pandas Index Values as List in Python
  • 2.  Using Python to Split String by Tab
  • 3.  Scroll Down Using Selenium in Python
  • 4.  How to Group By Columns and Find Standard Deviation in pandas
  • 5.  pandas idxmax – Find Index of Maximum Value of Series or DataFrame
  • 6.  math.degrees() Python – How to Convert Radians to Degrees in Python
  • 7.  How to Slice a Dictionary in Python
  • 8.  Using Selenium to Close Browser in Python
  • 9.  Truncate String in Python with String Slicing
  • 10.  Count Spaces in String in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy