Click here to Skip to main content
15,948,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I run This Code In windows 10 and Visual studio 2017 and it work Correctly, But when i Run This Code in windows 7 and Visual Studio 2015 Update 3 i Get Error :

"String was not recognized as a valid DateTime" .... in this line
C#
DateTime dt = DateTime.Parse(s, Thread.CurrentThread.CurrentCulture.DateTimeFormat);


How Can i Solved It :????

My Code:
C#
CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("fa-IR");
string s = "1396/02/31";
DateTime dt = DateTime.Parse(s, Thread.CurrentThread.CurrentCulture.DateTimeFormat);
Console.WriteLine(dt.ToShortDateString());
Thread.CurrentThread.CurrentCulture = originalCulture;
Console.ReadKey();
//error=String was not recognized as a valid DateTime.


What I have tried:

this link i find
https://msdn.microsoft.com/en-us/library/bb762911(v=vs.110).aspx
Posted
Updated 4-May-17 1:05am
v2

1 solution

Windows 10 added support for the Jalali calendar.
As a result, the DateTimeFormatInfo Class (System.Globalization)[^] class should return different date format strings for "fa-Ir" when called on Windows 10 and older versions (can not test it here).

At first you have to observe that your date string is Jalali while DateTime objects use the Gregorian calendar.

To get the Gregorian date from a Jalali date use the ToDateTime method of the PersianCalendar Class (System.Globalization)[^].

But before that conversion you have to parse the string. The first approach coming into mind is using DateTime.Parse with the fixed format "yyyy/MM/dd". But this will fail for specific dates. So you have to split the string into the year, month, and day parts.

Untested example without parse error checking:
C#
String[] dateParts = s.Split('/');
int year = int.Parse(dateParts[0]);
int month = int.Parse(dateParts[1]);
int day = int.Parse(dateParts[2]);
PersianCalendar pc = new PersianCalendar();
DateTime dt = pc.ToDateTime(year, month, day, 0, 0, 0, 0);
 
Share this answer
 
Comments
NorouziFar 6-May-17 0:32am    
I dont want convert it from jalali to Gregorian i want just change data type from syring to datetime

your code converted it to Gregorian
Jochen Arndt 6-May-17 3:50am    
The C# DateTime type can only represent Gregorian dates. You need a type that represents Jalali dates. And it is there in my example: The PersianCalendar class. pc.ToDateTime() intialises the date and returns the corresponding Gregorian date. If you don't want that, ignore the return value. Use the pc variable that is holding your Jalali date.
NorouziFar 7-May-17 0:18am    
i compare between window 10 and windows 7 and i find in windows 7 we don't have Hijri Calendar, if i find a way to add hijri calendar to windows 7 my problem to be solved

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900