This article explains python calculate 6 months ago from today. We will learn how datetime package helps in finding the exact date of 6 months from today. To get to know the procedure we will first have to get a know how of the datetime module and then we can get to the solution.
Understanding the datetime module
In python there is no specific data type for date. But the module datetime is used for dates in python. To initialize a date we can make an object of the datetime module. The syntax is as follows
import datetime
date= datetime.datetime.now()
print("the date today is : ", date)
The output of this code is
the date today is :Â 2022-10-02 19:57:03.353018
You can get the desired information by specifying the attribute, eg. if you just want to output the day you can do that by date.dayÂ
import datetime
date= datetime.datetime.now()
print("the date today is : ", date.year, date.month, date.day)
The output of this code is :
the date today is :Â 2022 10 2
Similarly you can output the time.
We will use this module to python calculate 6 months ago from today.
Understanding the relativedelta module
The relativedelta module helps in manipulation of the dates. It can help manipulate the time deltas, timezones, currencies. Which is why it is important for python calculate 6 months ago from today. The module has many functionalities like denoting the datetime of a string passed to it
from dateutil import parser
print (parser.parse("Monday,June 23, 2020, at 11:15pm"))
The output of the following code becomes
2020-06-23 23:15:00
Python code to calculate 6 months ago from today
Method-1: Using relativedelta module
To python calculate 6 months ago from today we use the following techniques.
- Import the datetime module to get todays date, from datetime import date.
- Use the
relativedelta
module, to go 6 months ago fromdateutil.relativedelta
importrelativedelta
- Get todays date and subtract the number of months that you want to go to, in my case it is 6 months add this
relativedelta
to todays date and you will get the date you want.
from datetime import date
from dateutil.relativedelta import relativedelta
# python calculate 6 months ago from today
six_months = date.today() + relativedelta(months=-6)
print("date from 6 months ago is : ",six_months ,"the day of the month is : ",six_months.day)
The output of this code is :
date from 6 months ago is :Â 2022-04-02 the day of the month is :Â 2
Method-2: Using replace() function
Another method is to use the replace function
- Get todays date.
- Create a function and pass it a date object and month.
- Add the months to the date month that has today’s date.
- Subtract the date month from the given date
- Replace the month
- New date would be the replaced month date
import datetime
today = datetime.date.today()
# python calculate 6 months ago from today
def sixMonthsFromToday(datesi, months = 0):
    sixMonth = months + datesi.month
    if sixMonth>12:
        sixMonth -=12
    return datesi.replace(month = sixMonth)
newdate = sixMonthsFromToday(today, 6)
print(newdate)
The output of this code is
2022-04-02
Conclusion
In this article we saw python calculate 6 months ago from today. Two methods are explained in the article. We also discussed the modules that are being used for datetime calculation and the relativedelta
that is used for manipulation of dates, currencies, time etc.
Further Reading