Search code examples
c#datetimeoverloadingobject-to-string

Using ToString on a DateTime object


In my database the date is stored like datetime but when I want to perform a search/filtering I want it to be based only on the date ignoring the exact time. I spend a lot of time figuring out how to do it and finally I got a working solution on my own :

string val = rule.Data;
if (!string.IsNullOrEmpty(val))
{
switch (rule.Field)
                    {
                        case "Date": {
                        DateTime parsedDate = DateTime.ParseExact(
                                val,
                                "dd/MM/yyyy",
                                CultureInfo.InvariantCulture);

                        var pYear = parsedDate.Year;
                        var pMonth = parsedDate.Month;
                        var pDay = parsedDate.Day;

rows = rows.Where(o => o.Date >= parsedDate && o.Date <= new DateTime(pYear, pMonth, pDay, 12, 59, 40)); break;
                        }
                    }

}

This is working Ok. It needs a little change but I think I can use the code above. However today a college of mine pass me a solution which is from a previous project being developed here, and this solution is a lot shorter and I would prefer to use it if possble. It looks like this:

        string val = rule.Data;
        if (!string.IsNullOrEmpty(val))
        {
            switch (rule.Field)
            {
            case "Date": { rows = rows.Where(o => o.Date.ToString("dd/MM/yyyy") == val); break; }
            }
        }

The code doesn't break when I try this but it's not filtering data too. I always get empty result. I guess that o.Date.ToString("dd/MM/yyyy") is where the problem lies. I don't know is it ok to use ToString() like this for DateTime object. In the example I'm using ToString() also get a format type like the one I'm providing here - ToString("dd/MM/yyyy") - but in my case ToString() is not overloaded anywhere. Is this a standard way to manipulate DateTime objects or I just can't find the place where ToStrin() is predefined. And finally, can you provide me with a working example based on the code above.


Solution

  • Depending on what culture o.Date is, Try:

        string val = rule.Data;
        if (!string.IsNullOrEmpty(val))
        {
            switch (rule.Field)
            {
                  case "Date":
                  {
                    rows = rows.Where(o => o.Date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture) == 
                                              DateTime.ParseExact(val,
                                              "dd/MM/yyyy",
                                              CultureInfo.InvariantCulture));
                    break;
                  }
            }
        }
    

    Or you could set the culture of the current thread instead:

    Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
    

    Edit: It should work if you avoid using strings:

    e.g.

    DateTime maxDate = new DateTime(2020, 11, 17);
    
    if (DateTime.Now.Date > maxDate)
    {
       // this will just work regardless of setting culture
    }