Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Date from Persian to Gregorian

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

like image 430
Mahdi Tahsildari Avatar asked Jun 27 '12 08:06

Mahdi Tahsildari


People also ask

What year is 2022 in Persian calendar?

For example, January 1, 2022 fell in year 1400 in the Solar Hijri calendar, which corresponds to year 1443 in the Hijri calendar.

What is the Persian date?

Piarom is one of the very popular kinds of semi-dried dates both in Iran and other countries. It is a black-brown narrow shaped fruit.


2 Answers

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.

like image 172
Jon Skeet Avatar answered Sep 23 '22 18:09

Jon Skeet


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(); 
like image 23
MohammadSoori Avatar answered Sep 20 '22 18:09

MohammadSoori