Python calculate 6 months ago from today [SOLVED]


Azka Iftikhar

Python

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.

  1. Import the datetime module to get todays date, from datetime import date.
  2. Use the relativedelta module, to go 6 months ago from dateutil.relativedelta import relativedelta
  3. 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

  1. Get todays date.
  2. Create a function and pass it a date object and month.
  3. Add the months to the date month that has today’s date.
  4. Subtract the date month from the given date
  5. Replace the month
  6. 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

  1. Python Oracle
  2. Python Date Time Formats

 

Views: 59

Azka Iftikhar

She is proficient in multiple programming languages, including C++, Golang, and Java, she brings a versatile skillset to tackle a wide array of challenges. Experienced Computer Scientist and Web Developer, she excels as a MERN Stack Expert. You can read more on her GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment


We try to offer easy-to-follow guides and tips on various topics such as Linux, Cloud Computing, Programming Languages, Ethical Hacking and much more.

Programming Languages

JavaScript

Python

Golang

Node.js

Java

Laravel