Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare between dates in .net

Tags:

c#

.net

datetime

I want to compare between two dates. From both the dates, I am fetching only date component using ToShortDateString(), as shown below. Now the problem is when I'm comparing the two dates. Its throwing error --

"Operator >= can't be applied to operands of type string and string."

DateTime srtdate = Convert.ToDateTime(allitem["StartDate"].Text.ToString());
DateTime srtdate = Convert.ToDateTime(allitem["StartDate"].Text.ToString());

 (DateTime.Now.ToShortDateString() >= srtdate.ToShortDateString()) 

I need to compare date component only, NOT date and time together.

Please suggest what is the alternative way. Thanks

To JON:-

(I went tyhrough all what you explained and understood hopefully what the point actually you trying to make. Just to clarify more and make a last check I ll show an example.) I have an web interface, where I give a start date and end date for a XYZ name (Note I can enter only date here, not time).

Start Date - 22-Feb-2012 AND End Date - 22-Feb-2012

Now in back end (code), if Start date and End date is same as Current date OR current date is in between start and end date, I want a ACTIVE flag set or else not. I give the condition as this:-

if ((DateTime.Today >= strdate.Date) && (DateTime.Today <= enddate.Date))
                    lblCondition.Text = "CHECKED";

Now when I debug the code,

Both DateTime.Today and strdate.Date gives the value as 2/22/2012 12:00:00 AM.

So, Jon my question is:- Would 'today' and 'date' work as per mentioned requirement, where only date component used. I hope it would.

Thanks a lot for all your explanantion before.

like image 295
Kings Avatar asked Oct 15 '25 09:10

Kings


1 Answers

Why are you converting to a string representation at all? If you only want to compare the date parts to two DateTime values, just use the Date property on each of them:

if (x.Date >= y.Date)

And the Today property is equivalent to DateTime.Now.Date.

Both Date and Today strip off the time part, leaving a time of midnight. It's not ideal that you've still got a type which is capable of representing times, but that's just the way the DateTime API works :(

Note that you should usually avoid using DateTime.Now or DateTime.Today in web applications unless you're really comfortable with it using the system default time zone as the day boundary. The user's idea of "today" may not be the same as the server's.

You should avoid using string conversions unless your goal is really to get a text representation.

Of course another alternative would be to use the date/time library I'm building, Noda Time, where you could use a LocalDate type - obviously that makes it clearer that you're only interested in the date and not the time.

EDIT: As the OP seems unconvinced that Date really does ignore the time component, here's an example:

using System;

public class Test
{
    static void Main()
    {
        // Two DateTime values with different times but
        // on the same date
        DateTime early = new DateTime(2012, 2, 22, 6, 0, 0);
        DateTime late = new DateTime(2012, 2, 22, 18, 0, 0);

        Console.WriteLine(early == late); // False
        Console.WriteLine(early.Date == late.Date); // True
    }    
}
like image 196
Jon Skeet Avatar answered Oct 16 '25 22:10

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!