Convert Date from Persian to Gregorian

44,363

Solution 1

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

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

you can use this code

return new DateTime(DT.Year,DT.Month,DT.Day,new System.Globolization.PersianCalendar());
Share:
44,363
Mahdi Tahsildari
Author by

Mahdi Tahsildari

Love programming, it's fun. MCP since 2011. C#, ASP.net MVC Core, SQL; NodeJs, Angular, HTML, CSS, Javascript. Currently focused on ASP.Net Core and Unit Testing.

Updated on July 09, 2022

Comments

  • Mahdi Tahsildari
    Mahdi Tahsildari almost 2 years

    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 ...

  • V4Vendetta
    V4Vendetta almost 12 years
    +1 Great ! ! Didn't know about the Calender provision in DateTime, thought GetYear, GetMonth were the ways to go
  • AminM
    AminM about 10 years
    what about ToDateTime method of PersianCalendar class??
  • Saeed Rahmani
    Saeed Rahmani over 7 years
    How to we can get with dash? mean "201-06-27 00:00:00"
  • Jon Skeet
    Jon Skeet over 7 years
    @Saeed: Just specify a custom format in the ToString call.
  • Saeed Rahmani
    Saeed Rahmani over 7 years
    @Jon: DateTime dateEC = new DateTime(datePersian.Year,datePersian.Month,datePersian.Day, new PersianCalendar()); I used this Code.
  • Jon Skeet
    Jon Skeet over 7 years
    @Saeed: I've already explained, you need to specify the format in the ToString call. There are lots of questions on SO about formatting.
  • Saeed Rahmani
    Saeed Rahmani over 7 years
    @Jon: Thank you. I specified ToString("yyyy-MM-dd hh:mm:ss") It's correct.
  • Jon Skeet
    Jon Skeet over 7 years
    @SaeedRahmani: You almost certainly want HH instead of hh, and I'd recommend explicitly specifying CultureInfo.InvariantCulture as well...
  • Meiki Neumann
    Meiki Neumann almost 5 years
    So simple and practical ...Thanks
  • faza
    faza over 4 years
    the default calendar of CultureInfo("fa-IR") is not 'Persian Calendar' in Windows 7. so ensure that this code works fine in windows 7 (if necessary)
  • Fereydoon Barikzehy
    Fereydoon Barikzehy over 4 years
    Does not work on server host because the default calendar is not Persian calendar
  • Jason Aller
    Jason Aller almost 4 years
    Welcome to Stack Overflow. Code only answers can almost always be improved by adding some explanation of how and why they work. When answering an older question with existing answers it is very important to point out what new aspect of the question your answer addresses. If the question is very old it can be useful to mention if the techniques you use were added sometime since the question was asked or if they rely upon a particular version of a language or program.
  • Jeremy Caney
    Jeremy Caney almost 4 years
    Also, so far as I can tell, while you're pulling your variables differently, this is in essence the same guidance as the accepted answer from eight years ago—but with far less explanation and a spelling error. When answering to old questions with accepted answers, please consider whether your answer is adding anything new—and, if it is, offer an explanation to readers of why your formulation might be preferable to the existing contributions. Do you mind updating your answer?
  • Mahdad Baghani
    Mahdad Baghani over 3 years
    This code neither complements/enhances the previous answers nor does it uses System.globalization.PersianCalendar that has been intended by the question. Please refer to comments on stackoverflow.com/a/62176697/6045793 to learn more about how to answer an old topic.
  • hamid reza shahshahani
    hamid reza shahshahani over 3 years
    thank you for attention me,but I use System.globalization.PersianCalendar in line miladyDate = new DateTime(year, month, day, new PersianCalendar()); and my code work for all conditions & win os. please see again stackoverflow.com/users/6045793/mahdad-baghani
  • Mahdad Baghani
    Mahdad Baghani over 3 years
    Yep, you are right about that. I must have missed the part you used System.globalization.PersianCalender. However, your answer still lacks enhancement to previous ones. Do not take this as a discouragement. I think that by applying a better naming (Shamsi -> Persian, Milady -> Gregorian) your answer can be upvoted. Moreover, it seems that you are using your own convention to parse the string date into the year, month and day variables, which may not be working in all cases.