Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Get the dates of Yesterday, Today, and Tomorrow using NumPy
Next article icon

Get Yesterday’s date using Python

Last Updated : 02 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Prerequisite: datetime module
In Python, date and time are not a data type of its own, but a module named datetime can be imported to work with the date as well as time. Datetime module comes built into Python, so there is no need to install it externally. 

To work with date, datetime module provides the date class and timedelta class is used to calculate differences in date. Let’s have a look at them.

  • Date class: When an object of this class is instantiated, it represents a date in the format YYYY-MM-DD. To return the current local date today() function of date class is used. today() function comes with several attributes (year, month and day). These can be printed individually.
    Syntax: 
date.today()
  • Timedelta class: Python timedelta() function is present under datetime library which is generally used for calculating differences in dates and also can be used for date manipulations in Python. It is one of the easiest ways to perform date manipulations.
    Syntax: 
datetime.timedelta(days=0, seconds=0, microseconds=0,                 milliseconds=0, minutes=0, hours=0, weeks=0)  Returns: Date

Below is the implementation 

Python3




# Python program to get
# Yesterday's date
 
 
# Import date and timedelta class
# from datetime module
from datetime import date
from datetime import timedelta
 
# Get today's date
today = date.today()
print("Today is: ", today)
 
# Yesterday date
yesterday = today - timedelta(days = 1)
print("Yesterday was: ", yesterday)
 
 

Output: 

Today is:  2019-12-11 Yesterday was:  2019-12-10

You just have to subtract no. of days using ‘timedelta’ that you want to get back in order to get the date from the past.

For example, on subtracting two we will get the date of the day before yesterday. 

Python3




# Python program to get
# Yesterday's date
 
 
# Import date and timedelta class
# from datetime module
from datetime import date
from datetime import timedelta
 
 
# Get today's date
today = date.today()
print("Today is: ", today)
 
# Get 2 days earlier
yesterday = today - timedelta(days = 2)
print("Day before yesterday was: ", yesterday)
 
 

Output:

Today is:  2019-12-11 Day before yesterday was:  2019-12-09

 



Next Article
Get the dates of Yesterday, Today, and Tomorrow using NumPy

A

ayushanandmallik
Improve
Article Tags :
  • Python
  • Python datetime-program
  • Python-datetime
Practice Tags :
  • python

Similar Reads

  • Get the dates of Yesterday, Today, and Tomorrow using NumPy
    In NumPy with the help of NumPy.datetime64('today', 'D'), we will find today date and if we want some date before today then we will subtract the no-of-date with the help of np.timedelta64() from today. And if we want some date after today then we will add the no of date with the help of np.timedelt
    1 min read
  • Get current date using Python
    Prerequisite: DateTime module In Python, Date and Time are not data types of their own, but a module named DateTime can be imported to work with the date as well as time. Datetime module comes built into Python, so there is no need to install it externally. The DateTime module provides some function
    3 min read
  • Get Current Timestamp Using Python
    A timestamp is a sequence of characters that represents the date and time at which a particular event occurred, often accurate to fractions of a second. Timestamps are essential in logging events, tracking files, and handling date-time data in various applications. There are 3 different ways to get
    2 min read
  • Python | Pandas Timestamp.date
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.date() function return a datetime object with same year, month and da
    2 min read
  • How to Get Current Year with Sys.Date() in R Shiny?
    Shiny is a powerful framework for building interactive web applications using the R programming Language. One common task in many applications is retrieving the current year. Whether we need it for a date picker, a timestamp, or a display of the current year, R makes it simple to get this informatio
    2 min read
  • How to Get Current Date and Time using Python
    In this article, we will cover different methods for getting data and time using the DateTime module and time module in Python. Different ways to get Current Date and Time using PythonCurrent time using DateTime objectGet time using the time moduleGet Current Date and Time Using the Datetime ModuleI
    6 min read
  • Working With Dates in Python
    In this article, we will discuss how to work with dates using python. Python makes dealing with dates and time very easy, all we have to do is import a module named DateTime that comes along with python. It is a more efficient way to work with date without the need of being explicitly programmed. By
    4 min read
  • Datetime to integer timestamp in Python
    A timestamp represents the number of seconds that have passed since January 1, 1970, 00:00:00 UTC (also known as the epoch). We can convert a datetime object to a timestamp using the built-in timestamp() method, and then round or typecast it to get an integer version. In this article, we'll learn ho
    3 min read
  • Python | Pandas Timestamp.weekday
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.weekday() function return the day of the week represented by the date
    2 min read
  • Python | Working with date and time using Pandas
    While working with data, encountering time series data is very usual. Pandas is a very useful tool while working with time series data.  Pandas provide a different set of tools using which we can perform all the necessary tasks on date-time data. Let's try to understand with the examples discussed b
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences