Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to check my dates date1 and date2 :
dates interval should be one calendar year

meaning for normal year it should check for 365 days but for leap year it shud check for 366 days.

when my date1 is 1st march 2012 and date2 is 28 th feb 2013


VB
If DateDiff(DateInterval.Day, date1, date2) > 365 Then
              
              Else
                
              End If
Posted

1 solution

Why?
I would do it by comparing date1.AddYears(1) with date2 - if it is less than or equal then it's within tolerance.
This way the DateTime takes care of leap years, and you don't have to worry about it.
 
Share this answer
 
Comments
sofia3 15-Jun-12 9:49am    
It will only add 1 year eg from 2012 to 2013
sofia3 15-Jun-12 9:50am    
but i want if my date1 is 1 march 2012 then date2 should be 28 feb 2013...plz help
OriginalGriff 15-Jun-12 10:04am    
So subtract a day as well...
DateTime dt1 = new DateTime(2012, 3, 1);
DateTime dt2 = dt1.AddYears(1).AddDays(-1);
Console.WriteLine("{0}\n{1}", dt1, dt2);
Results:
01/03/2012 00:00:00
28/02/2013 00:00:00

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900