I use the function below to convert Gregorian dates to Persian dates, but I\'ve been unable to write a function to do the reverse conversion. I want a function that converts
1- first install Persiandate NuGet package (17KB)
Install-Package PersianDate
2- use ToFa and toEn static methods of PersianDate class like(do not worry about the seperators and directions which will be handled by library itself as you can see in the following samples .also you can find the Usage Console project inside in GitHub to test it yourself easily):
//default format
string dts=ConvertDate.ToFa(DateTime.Now);//1393/08/01
//date only (short and D for Long)
dts=ConvertDate.ToFa(DateTime.Now, "d");//93/08/01
dts=ConvertDate.ToFa(DateTime.Now, "D");//پنج شنبه, 01 آبان 1393
//time only
dts=ConvertDate.ToFa(DateTime.Now, "t");//21:53
dts=ConvertDate.ToFa(DateTime.Now, "T");//21:53:26
//general short date + time
dts=ConvertDate.ToFa(DateTime.Now, "g");//93/08/01 21:53
dts=ConvertDate.ToFa(DateTime.Now, "G");//93/08/01 21:53:26
//general full date + time
dts=ConvertDate.ToFa(DateTime.Now, "f");//پنج شنبه, 01 آبان 1393 21:53
dts=ConvertDate.ToFa(DateTime.Now, "F");//پنج شنبه, 01 آبان 1393 21:53:26
//only month and year
dts=ConvertDate.ToFa(DateTime.Now, "m");//آبان 1
dts=ConvertDate.ToFa(DateTime.Now, "y");//1393 آبان
dts=ConvertDate.ToFa(DateTime.Now);//1393/08/01
// converting back to Gregorian date
Datetime dt= ConvertDate.ToEn(dts);//2014/10/23 00:00:00
3- Source code available in GitHub for any modifications and supporting new formats.
look for the following functions source code to see how it is generally working
public static DateTime ToEn(string fadate)
{
if (fadate.Trim() == "") return DateTime.MinValue;
int[] farsiPartArray = SplitRoozMahSalNew(fadate);
return new PersianCalendar().ToDateTime(farsiPartArray[0], farsiPartArray[1], farsiPartArray[2], 0, 0, 0, 0);
}
private static int[] SplitRoozMahSalNew(string farsiDate)
{
int pYear = 0;
int pMonth = 0;
int pDay = 0;
//normalize with one character
farsiDate = farsiDate.Trim().Replace(@"\", "/").Replace(@"-", "/").Replace(@"_", "/").
Replace(@",", "/").Replace(@".", "/").Replace(@" ", "/");
string[] rawValues = farsiDate.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (!farsiDate.Contains("/"))
{
if (rawValues.Length != 2)
throw new Exception("usually there should be 2 seperator for a complete date");
}
else //mostly given in all numeric format like 13930316
{
// detect year side and add slashes in right places and continue
}
//new simple method which emcompass below methods too
try
{
pYear = int.Parse(rawValues[0].TrimStart(new[] { '0' }).Trim());
pMonth = int.Parse(rawValues[1].TrimStart(new[] { '0' }).Trim());
pDay = int.Parse(rawValues[2].TrimStart(new[] { '0' }).Trim());
// the year usually must be larger than 90
//or for historic values rarely lower than 33 if 2 digit is given
if (pYear < 33 && pYear > 0)
{
//swap year and day
pYear = pDay;
pDay = int.Parse(rawValues[0]); //convert again
}
//fix 2 digits of persian strings
if (pYear.ToString(CultureInfo.InvariantCulture).Length == 2)
pYear = pYear + 1300;
//
if (pMonth <= 0 || pMonth >= 13)
throw new Exception("mahe shamsi must be under 12 ");
}
catch (Exception ex)
{
throw new Exception(
"invalid Persian date format: maybe all 3 numric Sal, Mah,rooz parts are not present. \r\n" + ex);
}
return new[] { pYear, pMonth, pDay };
}
This method converts a string which represents a shamsi date to System.DateTime :
public DateTime milady(string shamsiDate)
{
DateTime dt = new DateTime();
PersianCalendar pc = new PersianCalendar();
int pYear = Convert.ToInt32(shamsiDate.Substring(0, 4));
int pMonth =Convert.ToInt32(shamsiDate.Substring(5, 2));
int pDay = Convert.ToInt32( shamsiDate.Substring(8));
dt = pc.ToDateTime(pYear, pMonth, pDay,0,0,0,0);
return dt;
}
You can use PersianDateTime, using Nuget:
PM> Install-Package PersianDateTime
and how you can use:
DateTime miladiDate = new DateTime(2013, 5, 31);
PersianDateTime persianDate = new PersianDateTime(miladiDate);
and:
PersianDateTime persianDate = PersianDateTime.Parse("1392/03/02");
DateTime miladiDate = persianDate.ToDateTime();
for : more info about PersianDateTime