• 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 Minutes to Datetime Variable Using Python timedelta() Function

Add Minutes to Datetime Variable Using Python timedelta() Function

May 5, 2022 Leave a Comment

To add minutes 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_minute_in_future = now + timedelta(minutes=1)
sixty_minutes_in_future = now + timedelta(minutes=60)
one_day_in_future = now + timedelta(minutes=1440)

print(now)
print(one_minute_in_future)
print(sixty_minutes_in_future)
print(one_day_in_future)

#Output:
2022-05-05 15:45:53.655282
2022-05-05 15:46:53.655282
2022-05-05 16:45:53.655282
2022-05-06 15: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 minutes to a datetime variable.

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

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

Below is code that shows you how to add days to get some datetimes in the future using Python.

from datetime import timedelta, datetime

now = datetime.now()

one_minute_in_future = now + timedelta(minutes=1)
sixty_minutes_in_future = now + timedelta(minutes=60)
one_day_in_future = now + timedelta(minutes=1440)

print(now)
print(one_minute_in_future)
print(sixty_minutes_in_future)
print(one_day_in_future)

#Output:
2022-05-05 15:45:53.655282
2022-05-05 15:46:53.655282
2022-05-05 16:45:53.655282
2022-05-06 15: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 minutes, you can just subtract the call to the timedelta() function.

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

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

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

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

import pandas as pd

startdate = "05/05/2022"
enddate = pd.to_datetime(startdate) + pd.DateOffset(minutes=1)

print(startdate)
print(enddate)

#Output:
2022-05-05 00:00:00
2022-05-05 00:01:00

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

Other Articles You'll Also Like:

  • 1.  Using Python To Split String by Comma into List
  • 2.  Python Infinity – How to Use Infinity in Python
  • 3.  pandas set_value – Using at() Function to Set a Value in DataFrame
  • 4.  Python asinh – Find Hyperbolic Arcsine of Number Using math.asinh()
  • 5.  Using Selenium to Get Text from Element in Python
  • 6.  pandas Drop Rows – Delete Rows from DataFrame with drop()
  • 7.  pandas DataFrame size – Get Number of Elements in DataFrame or Series
  • 8.  Python os.sep – Create Operating System Path Seperator Character
  • 9.  Using Python to Print Variable Type
  • 10.  Check if pandas DataFrame is Empty 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