Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two persian date to find out which one is greater?

I want to compare two persian dates to find out which one is greater, I use this function :

public static List<MatchDrawHistory> GetAllMatchDrawHistory(string startDate, string endDate) 
{
    using (var db= new ReceiveSendEntitiesV5())
    {
        var matchDrawList = db.MatchDrawHistories.Where(x => String.CompareOrdinal(x.DrawStartDate,startDate)>=0 && String.CompareOrdinal(x.DrawEndDate , endDate) <=0).ToList();
        return matchDrawList;
    }
}

but it does not work, how can I do it?

EDIT: DrawStartDate and DrawStartDate are nvarchar(20) in DataBase, and these are persian date not gregorian date

like image 879
pejman Avatar asked Dec 07 '25 10:12

pejman


1 Answers

First you need to convert your string date to DateTime. Assuming your string date is as yyyy/MM/dd, the conversion function can be as follow:

private static DateTime ParseDate(string date)
{
    var pc = new PersianCalendar();
    string[] arrDate = date.Split("/");

     return pc.ToDateTime(Int32.Parse(arrDate[0]), Int32.Parse(arrDate[1]), 
        Int32.Parse(arrDate[2]), 0, 0, 0, 0);
}

Now you can use the following method to compare two dates:

private static bool Compare(DateTime firstDate, DateTime secondDate)
{
    return firstDate >= secondDate;
}
like image 179
majid zareei Avatar answered Dec 09 '25 23:12

majid zareei