无法从回历日期转换成公历日期(C#) [英] Cannot convert from Hijri Date to Gregorian date (c#)

查看:138
本文介绍了无法从回历日期转换成公历日期(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正与回历日期的工作,并试图将它们转换为使用如下代码公历日期:

Now i am working with Hijri dates and trying to convert them to Gregorian dates using the following code :

            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天的月份,如下列日期的所有日期:

this code is working fine with all dates except the date that has 30th day in month like the following :

'30 / 10/1433','30 /一千四百三十二分之一十二'或'30 / 05/1433'等.....所以如何处理和转换该日期与它对应的公历:•

'30/10/1433' , '30/12/1432' or '30/05/1433' etc ..... so how to handle and convert that date with its corresponding Gregorian :S

推荐答案

下面是现在运转良好
这个代码,我的代码'M从函数返回的日期字符串不是日期时间,但你可以简单地使用reuturn日期时间,而不是键入字符串

here is the code it is working well now on this code I'm returning the date from the function as string not as datetime, but you can simply using reuturn datetime type instead on string

    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));
}

要调用这个方法这里有一个例子

To call this Method here are an example

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



致电回历

to call the Hijri

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

这篇关于无法从回历日期转换成公历日期(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆