64

I want to convert a custom Gregorian date to Persian date in C#. For example, i have a string with this contents:

string GregorianDate = "Thursday, October 24, 2013";

Now i want to have:

string PersianDate = پنج‌شنبه 2 آبان 1392 ;

or

string PersianDate = 1392/08/02

Thanks

Ali Seyedi
  • 1,758
  • 1
  • 19
  • 24
JAC
  • 950
  • 1
  • 7
  • 11
  • To Help Persians I have Added some Library Here. 1. [PersianDateTime for .Net and .Net Core](https://github.com/Mds92/MD.PersianDateTime) 2. [Bootstrap PersianDateTimePicker](https://github.com/Mds92/MD.BootstrapPersianDateTimePicker) – Ali reza Soleimani Asl Dec 24 '22 at 15:13

7 Answers7

94

Use the PersianCalendar:

string GregorianDate = "Thursday, October 24, 2013";
DateTime d = DateTime.Parse(GregorianDate);
PersianCalendar pc = new PersianCalendar();
Console.WriteLine(string.Format("{0}/{1}/{2}", pc.GetYear(d), pc.GetMonth(d), pc.GetDayOfMonth(d)));
Alioza
  • 1,690
  • 12
  • 14
  • The answer is for 7 years ago. So I put Here [link](https://github.com/Mds92), updated some libraries. There are PersianDateTime and PersianDateTimePicker for .NET and .NET Core. For all dear Persians. – Ali reza Soleimani Asl Dec 24 '22 at 14:51
8
    DateTime date = new DateTime(2013, 10, 24);
    var calendar = new PersianCalendar();
    var persianDate = new DateTime(calendar.GetYear(date), calendar.GetMonth(date), calendar.GetDayOfMonth(date));
    var result = persianDate.ToString("yyyy MMM ddd", CultureInfo.GetCultureInfo("fa-Ir"));
Oleg
  • 1,378
  • 11
  • 22
6

You can use PersianDateTime:

PM> Install-Package PersianDateTime

The Reference: PersianDateTime

You can use string.Split() if you need customization.

Elnaz
  • 2,854
  • 3
  • 29
  • 41
4

The fowlling code should work in .net 4+ :

DateTime date=Convert.ToDateTime("2020-07-12T19:30:00.000Z");
string persianDateString = date.ToString("yyyy/MM/dd",new CultureInfo("fa-IR"));

persianDateString value would be:

1399/04/23
MSS
  • 3,520
  • 24
  • 29
3

Adding up to other answers, You can get the first one by using PersianDateTime:

var gregorianDate = "Thursday, October 24, 2013";
var date = DateTime.Parse(gregorianDate);
var persianDate = new PersianDateTime(date);
var result = persianDate.ToString("dddd d MMMM yyyy");
Ali Seyedi
  • 1,758
  • 1
  • 19
  • 24
0

In windows 10, using framework 4+:

Console.WriteLine(Convert.ToDateTime("1392/08/02",new CultureInfo("fa-IR")));

or

Console.WriteLine(DateTime.Parse("1392/08/02", new CultureInfo("fa-IR")));
Amir
  • 40
  • 2
0

You can convert a DateTime to Iran time using this method:

DateTime timeInIran = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTimeToConvert, "Iran Standard Time" );
AminSojoudi
  • 1,904
  • 2
  • 18
  • 42