Comparing Times
-
hello guys... I was thinking if there is any way to compare two times which are in different format. Like if I have these times
time1 = "01/12/2005";
time2 = "Dec 1, 2005";
//OR: time2 = "Dec 1, 05"
//OR: time2 = "December 1, 2005"
//OR: time2 = "12/01/2005"
//OR: all possible ways for time2Is it possible to compare these two times? Thanks
This world is going to explode due to international politics, SOON.
-
hello guys... I was thinking if there is any way to compare two times which are in different format. Like if I have these times
time1 = "01/12/2005";
time2 = "Dec 1, 2005";
//OR: time2 = "Dec 1, 05"
//OR: time2 = "December 1, 2005"
//OR: time2 = "12/01/2005"
//OR: all possible ways for time2Is it possible to compare these two times? Thanks
This world is going to explode due to international politics, SOON.
Yes. Never ever, ever try to compare dates, or any other numbers as strings: convert them to an appropriate class first. In this case, that class is DateTime:
DateTime dt1 = DateTime.Parse("01/12/2005");
DateTime dt2 = DateTime.Parse("Dec 12 2005");
TimeSpan diff = dt2 - dt1;You may need to use ParseExact, or TryParseExact if you know what format the date will be in to avoid problems like 03/04/05 - is that 3rd Apr 2005, or 4th March 2005, or 5th Apr 2003?
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
-
hello guys... I was thinking if there is any way to compare two times which are in different format. Like if I have these times
time1 = "01/12/2005";
time2 = "Dec 1, 2005";
//OR: time2 = "Dec 1, 05"
//OR: time2 = "December 1, 2005"
//OR: time2 = "12/01/2005"
//OR: all possible ways for time2Is it possible to compare these two times? Thanks
This world is going to explode due to international politics, SOON.
DateTime time1 = DateTime.Parse("01/12/2005"); DateTime time2 = DateTime.Parse("Dec 1, 2005"); if (time1==time2) {... } else { ... }
-
hello guys... I was thinking if there is any way to compare two times which are in different format. Like if I have these times
time1 = "01/12/2005";
time2 = "Dec 1, 2005";
//OR: time2 = "Dec 1, 05"
//OR: time2 = "December 1, 2005"
//OR: time2 = "12/01/2005"
//OR: all possible ways for time2Is it possible to compare these two times? Thanks
This world is going to explode due to international politics, SOON.
Hi, You can use Parse,TryParse,TryParseExact to parse your string to convert it into DateTime object. each have their own pros/cons. Here is the sample code for finding difference between dates.
DateTime dt1 = DateTime.Parse("firstDate");
DateTime dt2 = DateTime.Parse("secondDate");
TimeSpan dateDifference = dt1.Subtract(dt2);-Amit
-
Yes. Never ever, ever try to compare dates, or any other numbers as strings: convert them to an appropriate class first. In this case, that class is DateTime:
DateTime dt1 = DateTime.Parse("01/12/2005");
DateTime dt2 = DateTime.Parse("Dec 12 2005");
TimeSpan diff = dt2 - dt1;You may need to use ParseExact, or TryParseExact if you know what format the date will be in to avoid problems like 03/04/05 - is that 3rd Apr 2005, or 4th March 2005, or 5th Apr 2003?
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
Hey, i didn't seen your full reply. although both the code are same :)