Click here to Skip to main content
15,907,231 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir

I want to compare two dates which is in dd/MM/YYYY string format

Ankita Mali
Posted
Updated 1-Aug-11 18:37pm
v2

Simply convert the strings to DateTime objects, and then use the == operator to compare them:

C#
DateTime dt1;
DateTime dt2;

if (DateTime.TryParse(strDate1, out dt1) && DateTime.TryParse(strDate2, out dt2))
{
    if (dt1.Date == dt2.Date)
    {
        // the dates are identical
    }
}
 
Share this answer
 
Suppoese two textboxes as dtpReqDate and tpIssueDate.
DateTime(int year,int month,int day)
so break the string according to the above parameteres.

C#
DateTime dtEndDate = new DateTime(int.Parse(dtpReqDate.Text.Substring(6, 4)), int.Parse(dtpReqDate.Text.Substring(0, 2)), int.Parse(dtpReqDate.Text.Substring(3, 2)));
                    DateTime dtStartDate = new DateTime(int.Parse(dtpIssueDate.Text.Substring(6, 4)), int.Parse(dtpIssueDate.Text.Substring(0, 2)), int.Parse(dtpIssueDate.Text.Substring(3, 2)));
                    
                    TimeSpan devDate = dtStartDate.Subtract(dtEndDate);
 
Share this answer
 
v2
Comments
walterhevedeich 1-Aug-11 7:59am    
My 4. There's a shorter solution for it so that you dont need to go through all pain of the int conversions. You can do a Convert.ToDateTime, as others have pointed out, or DateTime.Parse.
Hi,
C#
if (date1.Equals(date2))
                   {


                   }

for checking equlaity..
do you want to take the diffrence in date?


for doing that convert those dates to datetime format and compare it

regards,
shefeek
 
Share this answer
 
v3
Comments
walterhevedeich 1-Aug-11 8:00am    
Correct.
#realJSOP 1-Aug-11 8:07am    
Your code is incorrect. The Equals method returns true if the objects are the same object (essentially, the same pointer value), not if the data represented by those objects is identical.
[no name] 1-Aug-11 8:19am    
You are wrong John. DateTime is a value type and it's Equal method has been overriden to compare values.
YYYYMMdd string format is easy to compare, but Permalink gives you the elegant and usefull way to compare dates
 
Share this answer
 
Comments
walterhevedeich 1-Aug-11 7:57am    
What's permalink?
Hi,
Compare after conversion.
Use Convert.ToDateTime("05/12/1984");
 
Share this answer
 
v2
Comments
walterhevedeich 1-Aug-11 7:55am    
Correct.
Ankita Mali 2-Aug-11 0:29am    
but my date is in dd/MM/yyyy format if the date is 28/05/2011 it is giving error
Convert both to DateTime and then use DateTime.Compare to do the comparison
 
Share this answer
 
v2
Comments
UJimbo 1-Aug-11 7:52am    
1 star? Really?
walterhevedeich 1-Aug-11 7:56am    
I think your solution is correct. Countered with a 5.
[no name] 1-Aug-11 8:15am    
There are trolls who keep downvoting answers without even understanding what it means. Countered with 5.
UJimbo 1-Aug-11 8:19am    
Thank you gentlemen
You can use DateTime.Parse()[^] method to convert the string to a DateTime value and then after that, the compare is pretty straightforward.
 
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