.NET How to parse a Persian / Farsi date string (Jalali calendar) into a DateTime object?

前端 未结 4 1843
醉梦人生
醉梦人生 2020-12-28 09:14

I\'ve run across several great code libraries for converting a Persian (Jalali calendar) date to a Gregorian date. However, my original source is a string, not a DateTime ob

相关标签:
4条回答
  • 2020-12-28 09:18

    For parsing the date string, I simply use the default calendar to get the date values (year, month, day, etc.)

    I then use this library here to convert the Persian date values to the Gregorian date values.

    My code now looks like this:

    string persianDate = "1390/02/07";
    CultureInfo persianCulture = new CultureInfo("fa-IR");
    DateTime persianDateTime = DateTime.ParseExact(persianDate, "yyyy/MM/dd", persianCulture);    // this parses the date as if it were Gregorian
    
    JalaliCalendar jc = new JalaliCalendar();
    // convert the Persian calendar date to Gregorian
    DateTime gregorianDateTime = jc.ToDateTime(persianDateTime.Year, persianDateTime.Month, persianDateTime.Day, persianDateTime.Hour, persianDateTime.Minute, persianDateTime.Second, persianDateTime.Millisecond);
    

    Of course, I'll have to take care of date components with names (months, days of the week) myself, but I can deal with that pretty easily.

    0 讨论(0)
  • 2020-12-28 09:21

    try it in ASP.NET MVC for validate Persian date

     [RegularExpression(@"^$|^([1۱][۰-۹ 0-9]{3}[/\/]([0 ۰][۱-۶ 1-6])[/\/]([0 ۰][۱-۹ 1-9]|[۱۲12][۰-۹ 0-9]|[3۳][01۰۱])|[1۱][۰-۹ 0-9]{3}[/\/]([۰0][۷-۹ 7-9]|[1۱][۰۱۲012])[/\/]([۰0][1-9 ۱-۹]|[12۱۲][0-9 ۰-۹]|(30|۳۰)))$", ErrorMessage = "Not Validate")]
    
    0 讨论(0)
  • 2020-12-28 09:22

    You can always use the native System.Globalization.PersianCalendar .NET class. but it is a bit tricky.

    Consider this code for converting from Jalali Date (here 1387/03/18) to Gregorian DateTime:

    System.Globalization.PersianCalendar persianCal = new System.Globalization.PersianCalendar();
        DateTime GregorianDate = persianCal.ToDateTime(1387, 3, 18, 12, 0, 0, 0);
    

    and the following code to convert a Gregorian DateTime (here 1983/08/03) to Persian Date string:

    DateTime GregorianDate = DateTime.Parse("1983/08/03");
    string FarsiDateConverted = persianCal.GetYear(GregorianDate).ToString("0000") + "/" +
                 persianCal.GetMonth(GregorianDate).ToString("00") + "/" +
                 persianCal.GetDayOfMonth(GregorianDate).ToString("00");
    

    just a note for the link provided by @Dan Bailiff I should repeat the author of the article's words:

    "The main purpose for using this JalaiCalendar in place of the .NET Framework's PersianCalendar must be the need of date conversation for historical events. If you just want to display the current date in your website, PersianCalendar is sufficient."

    In fact algorithm of .NET Framework is correct for the years 1178 to 1634 Jalali (1799 to 2256 Gregorian)

    Update:

    Starting with the .NET Framework 4.6, the PersianCalendar class uses the Hijri solar astronomical algorithm rather than an observational algorithm to calculate dates. This makes the PersianCalendar implementation consistent with the Persian calendar in use in Iran and Afghanistan, the two countries in which the Persian calendar is in most widespread use. So if you use >4.6 version of .NET Framework you don't need any other libraries for converting dates to/from Persian Calendar.

    0 讨论(0)
  • 2020-12-28 09:24

    Or:

    using System.Globalization;
    
    CultureInfo MyCultureInfo = new CultureInfo("fa-IR");
          string MyString = "1390/02/07";
          DateTime MyDateTime = DateTime.Parse(MyString, MyCultureInfo);
    

    There is more example: http://msdn.microsoft.com/en-us/library/2h3syy57.aspx#Y418

    0 讨论(0)
提交回复
热议问题