两个hijri日期之间的年,月和日的差异

时间:2012-01-03 09:44:09

标签: asp.net c#-4.0 time

请有人提供一种方法来预先计算2个hijri日期之间的差异

我试过这段代码

            HijriCalendar hijriCal=new HijriCalendar();
            DateTimeFormatInfo DTFormat = new System.Globalization.CultureInfo("ar-sa", false).DateTimeFormat;
            DTFormat.Calendar = new System.Globalization.HijriCalendar();
            DTFormat.ShortDatePattern = "dd/MM/yyyy";
            string HijriDate = FromDate.Date.ToString("d", DTFormat);
            string[] fromDateParams=HijriDate.Split('/');
            HijriDate = ToDate.Date.ToString("d", DTFormat);
            string[] toDateParams = HijriDate.Split('/');
            DateTime fromDateHijri = new DateTime(hijriCal.GetYear(FromDate), hijriCal.GetMonth(FromDate), int.Parse(fromDateParams[0]), hijriCal);
            DateTime toDateHijri = new DateTime(hijriCal.GetYear(ToDate), hijriCal.GetMonth(ToDate), int.Parse(toDateParams[0]), hijriCal);

            TimeSpan ts = ToDate.Subtract(FromDate);

1 个答案:

答案 0 :(得分:1)

只要您将日期存储在正常日期时间内,只需将其视为任何其他日期。

public TimeSpan GetDifference(this DateTime date1, DateTime date2) {
    if (date1 < date2) {
        return date2 - date1;
    }
    else if (date1 > date2) { 
        return date1 - date2;
    }
    return new TimeSpan(0);
}