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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / Python / Python Get Yesterday’s Date

Python Get Yesterday’s Date

February 8, 2022 Leave a Comment

To get yesterday’s date in Python, the easiest way is to use the Python timedelta() function from the datetime module.

from datetime import timedelta, date

yesterday_date = date.today() - timedelta(days=1)

print(date.today())
print(yesterday_date)

#Output:
2022-02-08
2022-02-07

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

One such change is to be able to get yesterday’s date from today’s date.

With Python, we can easily get yesterday’s date from the current date with the help of the datetime module.

To get yesterday’s date, we need to subtract 1 day from today’s date in Python. To do so, we can use the timedelta() function from the datetime module.

Below is code that shows you how to subtract 1 day from today’s date to get yesterday’s date in Python.

from datetime import timedelta, date

yesterday_date = date.today() - timedelta(days=1)

print(date.today())
print(yesterday_date)

#Output:
2022-02-08
2022-02-07

Adding One Day to Get Tomorrow’s Date Using Python

We can easily get tomorrow’s date using the Python datetime module. To get tomorrow’s date, we just need to add 1 day using the timedelta() function.

Below is the Python code which will allow you to get tomorrow’s date.

from datetime import timedelta, date

tomorrow_date = date.today() + timedelta(days=1)

print(date.today())
print(tomorrow_date)

#Output:
2022-02-08
2022-02-09

How to Get Yesterday’s Date With pandas in Python

If you are using the Python pandas module, we can get the date of yesterday easily.

With pandas, to subtract days to from a date, we use the DateOffset() function.

Below is an example of how to use pandas to get yesterday’s date in Python.

import pandas as pd

yesterday_date = pd.datetime.now() - pd.DateOffset(days=1)

Hopefully this article has been beneficial for you to learn how to get yesterday’s date using Python.

Other Articles You'll Also Like:

  • 1.  PROC LIFETEST Equivalent in Python
  • 2.  pandas fillna – Replace NaN in Dataframe using Python
  • 3.  Check if Variable is None in Python
  • 4.  Print Multiple Variables in Python
  • 5.  pandas Standard Deviation – Using std() to Find Standard Deviation
  • 6.  Remove Extension from Filename in Python
  • 7.  Python turtle dot() – Draw Dot on Turtle Screen
  • 8.  Check if Word is Palindrome Using Recursion with Python
  • 9.  Using Python to Create List of Prime Numbers
  • 10.  How to Save and Use Styles from Existing Word File With python-docx

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