• 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
  • VBA
  • 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.  Calculate Distance Between Two Points in Python
  • 2.  Count Letters in Word in Python
  • 3.  Using Python to Count Number of True in List
  • 4.  Selenium maximize_window() Function to Maximize Window in Python
  • 5.  Python Random Uniform – Generate Random Numbers Uniformly in Range
  • 6.  Using Lambda Expression with min() in Python
  • 7.  Sorting a List of Tuples by Second Element in Python
  • 8.  pandas DataFrame size – Get Number of Elements in DataFrame or Series
  • 9.  How to Group and Aggregate By Multiple Columns in Pandas
  • 10.  Python Check if Float is a Whole Number

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy