Convert Date from Persian to Gregorian
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());
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, 2022Comments
-
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 ...
-
Waihon Yew almost 12 yearsPersian or Gregorian seconds? ;-)
-
Habib almost 12 years
-
Iman over 9 yearsmy working answer to similar question stackoverflow.com/a/26543563/184572
-
-
V4Vendetta almost 12 years+1 Great ! ! Didn't know about the
Calender
provision inDateTime
, thoughtGetYear
,GetMonth
were the ways to go -
AminM about 10 yearswhat about
ToDateTime
method ofPersianCalendar
class?? -
Saeed Rahmani over 7 yearsHow to we can get with dash? mean "201-06-27 00:00:00"
-
Jon Skeet over 7 years@Saeed: Just specify a custom format in the ToString call.
-
Saeed Rahmani over 7 years@Jon:
DateTime dateEC = new DateTime(datePersian.Year,datePersian.Month,datePersian.Day, new PersianCalendar());
I used this Code. -
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 over 7 years@Jon: Thank you. I specified
ToString("yyyy-MM-dd hh:mm:ss")
It's correct. -
Jon Skeet over 7 years@SaeedRahmani: You almost certainly want
HH
instead ofhh
, and I'd recommend explicitly specifyingCultureInfo.InvariantCulture
as well... -
Meiki Neumann almost 5 yearsSo simple and practical ...Thanks
-
faza over 4 yearsthe 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 over 4 yearsDoes not work on server host because the default calendar is not Persian calendar
-
Jason Aller almost 4 yearsWelcome 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 almost 4 yearsAlso, 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 over 3 yearsThis 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 over 3 yearsthank 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 over 3 yearsYep, 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.