How to Find the Yesterday Date in Terminal using Python
In this tutorial, we’ll learn how to program "How to Find Yesterday's Date in the Terminal using Python." We’ll focus on calculating and displaying the correct date for yesterday. The objective is to accurately determine and print yesterday’s date in a straightforward and efficient manner. A sample program will be provided to guide you through the process, making it easy to follow and implement. So, let’s get started!
This topic is easy to understand. Simply follow the instructions I provide, and you’ll be able to complete it effortlessly. The program I’ll demonstrate shows the correct way to find yesterday’s date. I’ll also provide a simple and efficient method to accomplish this effectively. So, let’s start coding!
Getting Started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Creating Main Function
This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.- from datetime import datetime, timedelta
- presentday = datetime.now()
- yesterday = presentday - timedelta(1)
- while True:
- print("\n================= Find the Yesterday Date =================\n\n")
- print("Today: ", presentday.strftime('%d-%m-%Y'))
- print("\nYesterday: ", yesterday.strftime('%d-%m-%Y'))
- opt = input("\nDo you want to try again?(yes/no): ")
- if opt.lower() == 'yes':
- ret=False
- elif opt.lower() == 'no':
- ret=True
- print("Exiting program....")
- else:
- print("Please enter yes/no:")
- break
- if ret == False:
- continue
This program calculates and displays yesterday's date based on the current date using Python's datetime module. It starts by obtaining the current date and time with datetime.now(), then subtracts one day using timedelta(1) to determine yesterday's date. The dates are formatted in the DD-MM-YYYY format and displayed as "Today" and "Yesterday." The program includes a loop, allowing the user to run the process again or exit based on their input.
Output:
There you have it we successfully created How to Find the Yesterday Date in Terminal using Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language
Add new comment
- 38 views