Prerequisites: datetime module
We can handle Data objects by importing the module datetime and timedelta to work with dates.
- datetime module helps to find the present day by using the now() or today() method.
- timedelta class from datetime module helps to find the previous day date and next day date.
Syntax for timedelta:
class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
timedelta class is used because manipulating the date directly with increment and decrement will lead to a false Date. For example, if the present date is 31st of December, incrementing the date directly will only lead to the 32nd of December which is false. If we want to manipulate the Date directly first we have check day month and year combined and then increment accordingly. But, all this mess can be controlled by using timedelta class.
Syntax to find a present-day date:
datetime.now() Returns: A datetime object containing the current local date and time.
Syntax to find a previous-day and next-day date:
previous-day = datetime.now()-timedelta(1) next-day = datetime.now()+timedelta(1)
Find yesterday’s, today’s and tomorrow’s date
Python3
# Python program to find yesterday, # today and tomorrow # Import datetime and timedelta # class from datetime module from datetime import datetime, timedelta # Get today's date presentday = datetime.now() # or presentday = datetime.today() # Get Yesterday yesterday = presentday - timedelta( 1 ) # Get Tomorrow tomorrow = presentday + timedelta( 1 ) # strftime() is to format date according to # the need by converting them to string print ( "Yesterday = " , yesterday.strftime( '%d-%m-%Y' )) print ( "Today = " , presentday.strftime( '%d-%m-%Y' )) print ( "Tomorrow = " , tomorrow.strftime( '%d-%m-%Y' )) |
Yesterday = 10-12-2019 Today = 11-12-2019 Tomorrow = 12-12-2019
Time complexity: O(1)
Auxiliary space: O(1)
Find yesterday’s, today’s and tomorrow’s date Using calendar method
This approach uses the calendar module to get the name of the day of the week for yesterday’s, today’s and tomorrow’s date.
Algorithm:
1.Import the required modules calendar and date from datetime.
2.Get today’s date using the today() method of the date class.
3.Use the timedelta class to create a time delta of one day, which is then used to calculate yesterday’s and tomorrow’s date by subtracting or adding the delta to the current date, respectively.
4.Use the weekday() method to get the integer representation of the day of the week for yesterday’s, today’s and tomorrow’s date.
5.Use the day_name attribute of the calendar module to get the name of the day of the week for yesterday’s, today’s and tomorrow’s date.
6.Use the strftime() method to format the date in the desired format of ‘dd-mm-yyyy’.
7.Print the results to the console.
Python3
import calendar from datetime import date, timedelta today = date.today() yesterday = today - timedelta(days = 1 ) tomorrow = today + timedelta(days = 1 ) print ( "Yesterday = " , calendar.day_name[yesterday.weekday()], yesterday.strftime( '%d-%m-%Y' )) print ( "Today = " , calendar.day_name[today.weekday()], today.strftime( '%d-%m-%Y' )) print ( "Tomorrow = " , calendar.day_name[tomorrow.weekday()], tomorrow.strftime( '%d-%m-%Y' )) |
Yesterday = Thursday 13-04-2023 Today = Friday 14-04-2023 Tomorrow = Saturday 15-04-2023
Time complexity: O(1) – the time complexity is constant as the program only needs to perform simple date calculations and string formatting.
Space complexity: O(1) – the space complexity is constant as the program only needs to store a few date objects and strings.