Cannot convert from Hijri Date to Gregorian date (c#)

后端 未结 3 693
醉梦人生
醉梦人生 2020-12-07 02:05

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

string HijriDate;
string[] allFormats ={\"yyyy         


        
相关标签:
3条回答
  • 2020-12-07 02:21

    Max Value of a days in a month can be calculated by DateTime.DaysInMonth(year, month)

    and use it this way

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

    but

    int result = DateTime.DaysInMonth(2011, 2) // returns 28 being a non-leap year
    
    0 讨论(0)
  • 2020-12-07 02:24

    10th and 12nd months in Hirji don't have 30th day, so that date is invalid.

    0 讨论(0)
  • 2020-12-07 02:28

    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 return 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");
    
    0 讨论(0)
提交回复
热议问题