0

Although .net has a Persian Calendar, it cannot be used as a default calendar for current culture, because it's not one of the calendars returned by CultureInfo.OptionalCalendars. So, I'm storing them in strings.

How can I compare Persian Dates?

I'd like to compare different date formats, e.g., '1392/10/14' is less than '1394'. But if it's not possible, it's fine, I might be able to convert all of them to one format.

I don't imagine I can convert them to datetime, so how can I compare strings to get a similar result?

Akbari
  • 2,369
  • 7
  • 45
  • 85
  • `DateTime.TryParse` never recognizes Persian Dates as a valid date. Have you tried it yourself? – Akbari Jul 01 '15 at 09:22
  • 1
    You missed the second part of the answer - use `PersionCalendar` class. It just means you have to use it any time you're doing any datetime operations, but it will allow you to do comparisons etc. quite easily. You'll have to handle the parsing manually, though. – Luaan Jul 01 '15 at 09:24
  • Yes, but that `DateTime` object doesn't have a `TryParse`, am I missing something? – Akbari Jul 01 '15 at 09:28
  • And as for comparing strings, if you ensure all your dates are in the `yyyy/mm/dd` format, string comparison will work the same as proper datetime comparison. so you'll be fine. – Luaan Jul 01 '15 at 09:28

1 Answers1

0

As pointed by pm_2 in another question Try to use this:

static bool CheckDateGreater(string date1, string date2)
{
    DateTime dt1;
    if (!DateTime.TryParse(date1, out dt) return false;

    DateTime dt2;
    if (!DateTime.TryParse(date2, out dt) return false;

    return (dt1 >= dt2);
}

Then call:

var matchDrawList = db.MatchDrawHistories.Where(x => CheckDateGreater(x.DrawStartDate, startDate) && CheckDateGreater(endDate, x.DrawEndDate).ToList();
Community
  • 1
  • 1
Litisqe Kumar
  • 2,512
  • 4
  • 26
  • 40
  • I have noted in the comments that `Parse` always throws an exception, and `TryParse`'s output is `01/01/0001`. So, the Persian years aren't recognized a valid date. Have you tried it yourself? In which format? – Akbari Jul 02 '15 at 02:39