Reputation: 13582
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 ...
Upvotes: 64
Views: 55948
Reputation: 1
return persianCalendar.GetYear(date).ToString("0000")+"/"+persianCalendar.GetMonth(date).ToString("00")+"/"+persianCalendar.GetDayOfMonth(date).ToString("00");
Upvotes: -1
Reputation: 351
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);
Upvotes: 0
Reputation: 109
Because of unknown reason, none of solution worked for me. So I found a solution that worked.
Public Function ShamsiToMiladi(GDay As Long, GMonth As Long, GYear As Long) As Date
Dim PDay As Integer = New Date(GYear, GMonth, GDay, New System.Globalization.PersianCalendar).Day
Dim PMonth As Integer = New Date(GYear, GMonth, GDay, New System.Globalization.PersianCalendar).Month
Dim PYear As Integer = New Date(GYear, GMonth, GDay, New System.Globalization.PersianCalendar).Year
Return DateSerial(PYear, PMonth, PDay)
End Function
Upvotes: 0
Reputation: 61
install Nuget Package: Persian_Extention and use like this:
DateTime StartDate= StartDateStr.ToGregorianDateTime();
Or
String StartDateStr = StartDate.ToShamsiDate();
Upvotes: 0
Reputation: 31
you can use this code
return new DateTime(dt.Year,dt.Month,dt.Day,new System.Globalization.PersianCalendar());
Upvotes: 3
Reputation: 126
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;
}
Upvotes: 0
Reputation: 5405
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
Upvotes: 0
Reputation: 2408
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();
Upvotes: 24
Reputation: 1499860
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.
Upvotes: 114