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
adiijaiin
(Aditya Jain)
July 6, 2023, 1:44pm
2
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
postwick
(Paul Ostwick)
July 6, 2023, 1:49pm
4
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
adiijaiin
(Aditya Jain)
July 6, 2023, 1:52pm
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
adiijaiin
(Aditya Jain)
July 6, 2023, 1:53pm
7
Technically it should be false. 5 Months 28 days aren’t equal to 6 months
adiijaiin
(Aditya Jain)
July 6, 2023, 1:58pm
8
Either go by
DateInterval.Day
DateDiff(DateInterval.Day,Datetime.ParseExact(“01/01/2023”,“MM/dd/yyyy”, System.Globalization.CultureInfo.InvariantCulture).Date,Now.date)>=180
ppr
(Peter Preuss)
July 6, 2023, 1:58pm
9
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.
adiijaiin
(Aditya Jain)
July 6, 2023, 2:00pm
11
adiijaiin:
Either go by
DateInterval.Day
DateDiff(DateInterval.Day,Datetime.ParseExact(“01/01/2023”,“MM/dd/yyyy”, System.Globalization.CultureInfo.InvariantCulture).Date,Now.date)>=180
Go by Count of Days. That would be better. It would help you in getting accurate data as per condition
1 Like
postwick
(Paul Ostwick)
July 6, 2023, 2:02pm
12
That’s not incorrect. 5 months 28 days is not greater than 6 months.
postwick
(Paul Ostwick)
July 6, 2023, 2:03pm
13
That’s not really accurate since months have a different number of days.
ppr
(Peter Preuss)
July 6, 2023, 2:06pm
14
adiijaiin:
180
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
postwick
(Paul Ostwick)
July 6, 2023, 2:20pm
16
180 days isn’t always 6 months. Each month doesn’t have exactly 30 days.
adiijaiin
(Aditya Jain)
July 6, 2023, 2:25pm
17
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.
postwick
(Paul Ostwick)
July 6, 2023, 2:26pm
18
If they want 6 months, the correct solution is to count months and do the conditions properly.
adiijaiin
(Aditya Jain)
July 6, 2023, 2:26pm
19
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