I use the following code for convert the date from String
string JoiningDate="30/11/2013";
string[] dateconvert = JoiningDate.Split('/');
string newdate = dateconvert[1] + '/' + dateconvert[0] + '/' + dateconvert[2];
DateTime JoinDate = Convert.ToDateTime(newdate);
My System Date Format is:07/01/14.But I have the Following Error.
String was not recognized as a valid DateTime.
Then I change My system format is 01-jul-2014.It was working Fine.
Now,How to convert the date from string without consider the system Format?
Use DateTime.ParseExact
, specify the format, and specify the invariant culture to make it absolutely non-system-dependent:
DateTime dateTime = DateTime.ParseExact(text, "MM/dd/yyyy",
CultureInfo.InvariantCulture);
(If you're able to choose this format yourself, I'd use yyyy-MM-dd
instead, e.g. 2013-11-30. That's ISO-8601 compliant, and is clear to anyone technical... whereas something like 07/01/2014 looks like January 7th to lots of people in the world...)
See more on this question at Stackoverflow