How to convert a Persian date into a Gregorian date?

前端 未结 9 1226
萌比男神i
萌比男神i 2020-12-30 04:30

I use the function below to convert Gregorian dates to Persian dates, but I\'ve been unable to write a function to do the reverse conversion. I want a function that converts

相关标签:
9条回答
  • 2020-12-30 05:09

    DateTime is always in the Gregorian calendar, effectively. Even if you create an instance specifying a dfferent calendar, the values returned by the Day, Month, Year etc properties are in the Gregorian calendar.

    As an example, take the start of the Islamic calendar:

    using System;
    using System.Globalization;
    
    class Test
    {
        static void Main()
        {
            DateTime epoch = new DateTime(1, 1, 1, new HijriCalendar());
            Console.WriteLine(epoch.Year);  // 622
            Console.WriteLine(epoch.Month); // 7
            Console.WriteLine(epoch.Day);   // 18
        }
    }
    

    It's not clear how you're creating the input to this method, or whether you should really be converting it to a string format. (Or why you're not using the built-in string formatters.)

    It could be that you can just use:

    public static string FormatDateTimeAsGregorian(DateTime input)
    {
        return input.ToString("yyyy'/'MM'/'dd' 'HH':'mm':'ss",
                              CultureInfo.InvariantCulture);
    }
    

    That will work for any DateTime which has been created appropriately - but we don't know what you've done before this.

    Sample:

    using System;
    using System.Globalization;
    
    class Test
    {
        static void Main()
        {
            DateTime epoch = new DateTime(1, 1, 1, new PersianCalendar());
            // Prints 0622/03/21 00:00:00
            Console.WriteLine(FormatDateTimeAsGregorian(epoch));
        }
    
        public static string FormatDateTimeAsGregorian(DateTime input)
        {
            return input.ToString("yyyy'/'MM'/'dd' 'HH':'mm':'ss",
                                  CultureInfo.InvariantCulture);
        }
    }
    

    Now if you're not specifying the calendar when you create the DateTime, then you're not really creating a Persian date at all.

    If you want dates that keep track of their calendar system, you can use my Noda Time project, which now supports the Persian calendar:

    // In Noda Time 2.0 you'd just use CalendarSystem.Persian
    var persianDate = new LocalDate(1390, 7, 18, CalendarSystem.GetPersianCalendar());
    var gregorianDate = persianDate.WithCalendar(CalendarSystem.Iso);
    
    0 讨论(0)
  • 2020-12-30 05:09

    Use this code to convert Persian to Gregorian date and vice versa.

    public static class PersainDate
    {
        public static DateTime ConvertToGregorian(this DateTime obj)
        {
            DateTime dt = new DateTime(obj.Year, obj.Month, obj.Day, new PersianCalendar());
            return dt;
        }
        public static DateTime ConvertToPersian(this DateTime obj)
        {
            var persian = new PersianCalendar();
            var year = persian.GetYear(obj);
            var month = persian.GetMonth(obj);
            var day = persian.GetDayOfMonth(obj);
            DateTime persiandate = new DateTime(year, month, day);
            return persianDate;
        }
    }
    

    Use the code this way

            var time = DateTime.Now;
            var persianDate = time.ConverToPersian();
            var gregorianDate = persianDate.ConverToGregorian();
    
    0 讨论(0)
  • 2020-12-30 05:10
    You may try with this: 
    
    using System.Globalization // namespace
    GregorianCalendar greg = new GregorianCalendar();
    DateTime DayYearMonth = new DateTime(1433, 10, 11, greg );
    
    Console.WriteLine(greg .GetDayOfMonth(DayYearMonth )); // 11
    Console.WriteLine(greg .GetYear(DayYearMonth )); // 1433
    Console.WriteLine(greg .GetMonth(DayYearMonth )); // 10
    
    0 讨论(0)
  • 2020-12-30 05:15

    Thanks guys,I used below code and my problem solved:

    public static DateTime GetEdate(string _Fdate)
    {
        DateTime fdate = Convert.ToDateTime(_Fdate);
        GregorianCalendar gcalendar = new GregorianCalendar();
        DateTime eDate = pcalendar.ToDateTime(
               gcalendar.GetYear(fdate),
               gcalendar.GetMonth(fdate),
               gcalendar.GetDayOfMonth(fdate),
               gcalendar.GetHour(fdate),
               gcalendar.GetMinute(fdate),
               gcalendar.GetSecond(fdate), 0);
        return eDate;
    }
    
    0 讨论(0)
  • 2020-12-30 05:21

    Using Persia.NET Core can be of help to you:

    Converting Gregorian to Shamsi

    Persia.SolarDate solarDate = Persia.Calendar.ConvertToPersian(DateTime.Now);
    // getting the simple format of persian date
    string str = solarDate.ToString();
    

    Converting Shamsi to Gregorian

    DateTime ConvertToGregorian(SolarDate solarDate)
    DateTime ConvertToGregorian(LunarDate lunarDate)
    DateTime ConvertToGregorian(int year, int month, int day, DateType dateType)
    DateTime ConvertToGregorian(int year, int month, int day, int hour, int minute, int
    second, DateType dateType)
    

    You can download it here If your application is not supported .NET Core you can find the older version of this from NuGet.

    0 讨论(0)
  • 2020-12-30 05:25

    This method check leap years.

    private static DateTime JalaliToGregorianDate(string jalaliDate)
    {
        string[] JalaliDateArray = jalaliDate.Split(new Char[] { '/' });
        int Year, Mounth, Day;
        DateTime GregorianDate;
        PersianCalendar persianCalendar = new PersianCalendar();
        //
        Year = int.Parse(JalaliDateArray[2]);
        if (Year < 100)
        {
            Year = 1300 + Year;
        }
        Mounth = int.Parse(JalaliDateArray[1]);
        Day = int.Parse(JalaliDateArray[0]);
        if (Day > 29 && Mounth == 12)
        {
            if (!persianCalendar.IsLeapYear(Year))
            {
                throw (new Exception(string.Format("IsNotLeapYear&{0}", Year)));
            }
        }
        GregorianDate = persianCalendar.ToDateTime(Year, Mounth, Day, 0, 0, 0, 0);
        //
        return GregorianDate;
    }
    
    0 讨论(0)
提交回复
热议问题