Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to compare two dates excluding years.

Ex.

Input: FromDate: 01 March, ToDate: 05 March

then all records between these two dates should be come whether there is any year. It does not matter.

So please anyone can help me in this issue.

Thanks in advance.
Posted
Comments
Richard MacCutchan 6-Mar-14 7:57am    
Set the year to any value. As long as both are the same it will work, unless the ToDate is in the following year. But that is easy to check for.
Matt T Heffron 6-Mar-14 14:23pm    
It looks ("then all records between these two dates") like (s)he needs to filter other info based on the two inputs.
If comparing in SQL, this could be "interesting"...
If records are in memory, then writing a MagicBetween method could be done (and probably use Linq to the collection).

You could artifically introduce the year for your comparison. E.g.
string s1 = "15/03";
string s2 = "01/03";
DateTime d1 = DateTime.Parse(s1 + "/2014");
DateTime d2 = DateTime.Parse(s2 + "/2014");
TimeSpan t = d1.Subtract(d2);


You would need to ensure that s1 is always the later "date" and you may want to consider the overlap over the end of the year e.g. 20 December to 03 January. But the principle would still work - just choose an arbitrary year.

Watch out for leap years too
 
Share this answer
 
in c# you can saperate the day part and month part of both the dates and can compare that and leave year as it is.

C#
Datetime d1 = somedate;
Datetime d2 = someotherdate;

int day1 = d1.Day;
int day2 = d2.Day;

int Month1 = d1.Month;
int Month2 = d2.Month;
 if(day1 == day2 && Month1 == Month2)
{
//your logic goes here 
}
else 
{
//Date not matching...
}
 
Share this answer
 

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