2

I am developing an asp.net web site, I want to convert a string of Persian date to DateTime type and I used below code but I get Gregorian date

System.Globalization.CultureInfo pr = new System.Globalization.CultureInfo("fa-ir");
DateTime dt = DateTime.ParseExact("1396/04/31", "yyyy/MM/dd", pr);

dt is 7/22/2017 but it must be 1396/04/31

I try this code too but have the same problem

PersianCalendar persianDate = new PersianCalendar();
DateTime st = persianDate.ToDateTime(1396, 04, 31, 0, 0, 0, 0);

I use windows 10 and .net 4.5

how can I resolve this problem. please help me.

saeid6366
  • 33
  • 7
  • [`System.DateTime` represents dates in the Gregorian calendar](https://msdn.microsoft.com/en-us/library/system.datetime#Remarks), so you can't use it to represent other calendars. – Slai May 08 '17 at 01:55

1 Answers1

4

you can use PersianCalendar

System.Globalization.CultureInfo pr = new System.Globalization.CultureInfo("fa-ir");
        DateTime dt = DateTime.ParseExact("1396/04/31", "yyyy/MM/dd", pr);
        PersianCalendar pc = new PersianCalendar();
        Console.WriteLine("Today in the Persian Calendar:    {0}, {1}/{2}/{3} {4}:{5}:{6}\n",
            pc.GetDayOfWeek(dt),
            pc.GetYear(dt),
            pc.GetMonth(dt),
            pc.GetDayOfMonth(dt),
            pc.GetHour(dt),
            pc.GetMinute(dt),
            pc.GetSecond(dt));
Rainmaker
  • 583
  • 3
  • 13