无法从Hijri Date转换为Gregorian date(c#)

时间:2012-06-25 12:56:37

标签: c# date hijri

现在我正在使用Hijri日期并尝试使用以下代码将它们转换为格里高利日期:

            string HijriDate;
            string[] allFormats ={"yyyy/MM/dd","yyyy/M/d",
                "dd/MM/yyyy","d/M/yyyy",
                "dd/M/yyyy","d/MM/yyyy","yyyy-MM-dd",
                "yyyy-M-d","dd-MM-yyyy","d-M-yyyy",
                "dd-M-yyyy","d-MM-yyyy","yyyy MM dd",
                "yyyy M d","dd MM yyyy","d M yyyy",
                "dd M yyyy","d MM yyyy","MM/dd/yyyy"};
            CultureInfo enCul = new CultureInfo("en-US");
            CultureInfo arCul = new CultureInfo("ar-SA");
            arCul.DateTimeFormat.Calendar = new System.Globalization.HijriCalendar(); 
            DateTime tempDate = DateTime.ParseExact(HijriDate, allFormats, arCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
            return tempDate.ToString("MM/dd/yyyy");

此代码适用于除日期之外的所有日期,但下列日期为30天:

'30 / 10/1433','30 / 12/1432'或'30 / 05/1433'等.....所以如何处理和转换该日期及其相应的Gregorian:S

3 个答案:

答案 0 :(得分:4)

这是它运作良好的代码 现在在这段代码中,我将函数的日期作为字符串而不是日期时间返回,但您可以简单地使用reuturn日期时间类型而不是字符串

    public string ConvertDateCalendar(DateTime DateConv, string Calendar, string DateLangCulture)
{
    System.Globalization.DateTimeFormatInfo DTFormat;
    DateLangCulture = DateLangCulture.ToLower();
    /// We can't have the hijri date writen in English. We will get a runtime error - LAITH - 11/13/2005 1:01:45 PM -

    if (Calendar == "Hijri" && DateLangCulture.StartsWith("en-"))
    {
        DateLangCulture = "ar-sa";
    }

    /// Set the date time format to the given culture - LAITH - 11/13/2005 1:04:22 PM -
    DTFormat = new System.Globalization.CultureInfo(DateLangCulture, false).DateTimeFormat;

    /// Set the calendar property of the date time format to the given calendar - LAITH - 11/13/2005 1:04:52 PM -
    switch (Calendar)
    {
        case "Hijri":
            DTFormat.Calendar = new System.Globalization.HijriCalendar();
            break;

        case "Gregorian":
            DTFormat.Calendar = new System.Globalization.GregorianCalendar();
            break;

        default:
            return "";
    }

    /// We format the date structure to whatever we want - LAITH - 11/13/2005 1:05:39 PM -
    DTFormat.ShortDatePattern = "dd/MM/yyyy";
    return (DateConv.Date.ToString("f", DTFormat));
}

此处调用此方法是一个示例

    ltrCalValue.Text = ConvertDateCalendar(CalHijri.SelectedDate, "Gregorian", "en-US");

致电Hijri

    ltrCalValue.Text = ConvertDateCalendar(CalHijri.SelectedDate, "Hijri", "en-US");

答案 1 :(得分:0)

一个月中某天的最大值可以通过DateTime.DaysInMonth(year, month)

计算得出

并以这种方式使用

int result = DateTime.DaysInMonth(2012, 2); // returns 29 being a leap year

但是

int result = DateTime.DaysInMonth(2011, 2) // returns 28 being a non-leap year

答案 2 :(得分:-1)

Hirji的第10个月和第12个月没有第30天,因此该日期无效。