Convert Date from Persian to Gregorian

C#.NetDatetimeCalendar

C# Problem Overview


How can I convert Persian date to Gregorian date using System.globalization.PersianCalendar? Please note that I want to convert my Persian Date (e.g. today is 1391/04/07) and get the Gregorian Date result which will be 06/27/2012 in this case. I'm counting seconds for an answer ...

C# Solutions


Solution 1 - C#

It's pretty simple actually:

// I'm assuming that 1391 is the year, 4 is the month and 7 is the day
DateTime dt = new DateTime(1391, 4, 7, persianCalendar);
// Now use DateTime, which is always in the Gregorian calendar

When you call the DateTime constructor and pass in a Calendar, it converts it for you - so dt.Year would be 2012 in this case. If you want to go the other way, you need to construct the appropriate DateTime then use Calendar.GetYear(DateTime) etc.

Short but complete program:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        PersianCalendar pc = new PersianCalendar();
        DateTime dt = new DateTime(1391, 4, 7, pc);
        Console.WriteLine(dt.ToString(CultureInfo.InvariantCulture));
    }
}

That prints 06/27/2012 00:00:00.

Solution 2 - C#

You can use this code to convert Persian Date to Gregorian.

// Persian Date
var value = "1396/11/27";
// Convert to Miladi
DateTime dt = DateTime.Parse(value, new CultureInfo("fa-IR"));
// Get Utc Date
var dt_utc = dt.ToUniversalTime();

Solution 3 - C#

you can use this code

return new DateTime(DT.Year,DT.Month,DT.Day,new System.Globolization.PersianCalendar());

Solution 4 - C#

I have an extension method for this:

 public static DateTime PersianDateStringToDateTime(this string persianDate)
 {
        PersianCalendar pc = new PersianCalendar();

        var persianDateSplitedParts = persianDate.Split('/');
        DateTime dateTime = new DateTime(int.Parse(persianDateSplitedParts[0]), int.Parse(persianDateSplitedParts[1]), int.Parse(persianDateSplitedParts[2]), pc);
        return DateTime.Parse(dateTime.ToString(CultureInfo.CreateSpecificCulture("en-US")));
 }

For more formats and culture-specific formats

Example: Convert 1391/04/07 to 06/27/2012

Solution 5 - C#

i test this code on windows 7 & 10 and run nicely without any problem

    /// <summary>
    /// Converts a Shamsi Date To Milady Date
    /// </summary>
    /// <param name="shamsiDate">string value in format "yyyy/mm/dd" or "yyyy-mm-dd"                     
     ///as shamsi date </param>
    /// <returns>return a DateTime in standard date format </returns>
public static DateTime? ShamsiToMilady(string shamsiDate)
    {
        if (string.IsNullOrEmpty(shamsiDate))
            return null;

        string[] datepart = shamsiDate.Split(new char[] { '/', '-' });
        if (datepart.Length != 3)
            return null;
        // 139p/k/b validation
        int year = 0, month = 0, day = 0;
        DateTime miladyDate;
        try
        {
            year = datepart[0].Length == 4 ? int.Parse(datepart[0]) : 
                                    int.Parse(datepart[2]);
            month = int.Parse(datepart[1]);
            day = datepart[0].Length == 4 ? int.Parse(datepart[2]) : 
                               int.Parse(datepart[0]);
            var currentCulture = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            miladyDate = new DateTime(year, month, day, new PersianCalendar());
            Thread.CurrentThread.CurrentCulture = currentCulture;
        }
        catch
        {
            return null;
        }

        return miladyDate;
    }

Solution 6 - C#

To convert from Gregorian date to Persian(Iranian) date:

    string GetPersianDateYear(string PersianDate)
    {
       return PersianDate.Substring(0, 4);
    }
    
    string GetPersianDateMonth(string PersianDate)
    {
        if (PersianDate.Trim().Length > 8 || PersianDate.IndexOf('/') > 0 || PersianDate.IndexOf('-') > 0)
        {
            return PersianDate.Substring(5, 2);
        }
        else
        {
            return PersianDate.Substring(4, 2);
        }
    }
    
    string GetPersianDateDay(string PersianDate)
    {
         if (PersianDate.Trim().Length > 8 || PersianDate.IndexOf('/') > 0 || PersianDate.IndexOf('-') > 0)
         {
             return PersianDate.Substring(8, 2);
         }
         else
         {
             return PersianDate.Substring(6, 2);
         }
     }
    
     var PersianDate = "1348/10/01";
     var perDate = new System.Globalization.PersianCalendar();
     var dt = perDate.ToDateTime(GetPersianDateYear(PersianDate))
                                            , int.Parse(GetPersianDateMonth(PersianDate))
                                            , int.Parse(GetPersianDateDay(PersianDate)), 0, 0, 0, 0);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMahdi TahsildariView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#MohammadSooriView Answer on Stackoverflow
Solution 3 - C#MahdiView Answer on Stackoverflow
Solution 4 - C#Fereydoon BarikzehyView Answer on Stackoverflow
Solution 5 - C#hamid reza shahshahaniView Answer on Stackoverflow
Solution 6 - C#gyousefiView Answer on Stackoverflow