How to find the 6 months date difference between two dates

I have used this method to find the difference month difference between two dates:

DateDiff(DateInterval.Month,Date time.ParseExact(Date,“MM/dd/yyy”, System.Globalization.CultureInfo.InvariantCulture).Date,Now.date)>6

If the input variable Date=01/19/2023

Today date is 07/06/2023

The month difference between two dates is 5 month 18 days ,so the above code is working fine.

If the input variable Date=01/01/2023

Today date is 07/06/2023

The month difference between two dates is 6 month 4 days ,it is not working and it is going into less than 6 months category

Hi @sruthesanju

Did you put the conditioning right?
You can reverse the condition if what you want is the reciprocal of what is currently happening in the code.

Can you please explain in detail with code logic and examples I have given

6 months 4 days is 6 months. 6 is not greater than 6. You don’t have a “less than 6” category, you have a “more than 6” and a “not more than 6” category which includes “equals 6”

Change your condition to >= or change it to >5

Hi @sruthesanju,

Can you just use the condition as:

DateDiff(DateInterval.Month,Datetime.ParseExact(“01/19/2023”,“MM/dd/yyyy”, System.Globalization.CultureInfo.InvariantCulture).Date,Now.date)>=6

If I put >=6 first condition is getting as incorrect since it is 5 months 28 days

Technically it should be false. 5 Months 28 days aren’t equal to 6 months

Either go by

DateInterval.Day

DateDiff(DateInterval.Day,Datetime.ParseExact(“01/01/2023”,“MM/dd/yyyy”, System.Globalization.CultureInfo.InvariantCulture).Date,Now.date)>=180

It sounds like the fraction of a month count will be important for your check expectations
DateDiff is not working at this detail level
But technically it should be implementable with a custom approach

How to get the correct condition for for this issue.

Go by Count of Days. That would be better. It would help you in getting accurate data as per condition

1 Like

That’s not incorrect. 5 months 28 days is not greater than 6 months.

That’s not really accurate since months have a different number of days.

we hope you agree on:

in 2022 JAN,FEB,MAR,APR,MAI,JUN = 6 Months
then 31+28+31+30+31+30 = 181
In Leap Years + 1
As month do have different counts we would avoid to general Month=30days factors when the exactr result is needed

1 Like

Its working of if I used 180 days.thanks for the quick support

180 days isn’t always 6 months. Each month doesn’t have exactly 30 days.

Hi @postwick : At minimum on any given year in 6 months there would be 181 days
Non Leap Year: 31+28+31+30+31+30 should result in 181

We are going on logic if at minimum the days difference is 180 days, that’s the closest option possible to what the user wants to achieve.

If they want 6 months, the correct solution is to count months and do the conditions properly.

absolutely agreed on that front but average month days is 30, based on this assumption, we can move forward.

if dateA > date B then
if dateA.addMonths(-6) > date B then
--> more than 6 months
else
--> less than 6 months
end if
else
if dateA.addMonths(6) < date B then
--> more than 6 months
else
--> less than 6 months
end if
end if