Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Hi,

DateTime.Parse is not working.
I have written follwing code.

What I have tried:

DateTime.Parse("29/12/2016", "en");

same code is working for other culture. only for english its giving follwing error.

String was not recognized as a valid DateTime.</pre>



Thanks......
Posted
Updated 27-Dec-16 20:11pm
v2

1 solution

According to DateTime.Parse Method (String, IFormatProvider) (System)[^], the second parameter is IFormatProvider[^].
Try
C#
using System;
using System.Globalization;

public class Program
{
	public static void Main()
	{
		DateTime dt = DateTime.Parse("29/12/2016", new CultureInfo("en-GB"));

		Console.WriteLine(dt);

	}
}
 
Share this answer
 
Comments
Maciej Los 28-Dec-16 2:33am    
5.
viprat 28-Dec-16 2:54am    
thanks... above code is working.. but why its not working with only "en". what is the difference between "en" and "en-GB"??
Peter Leow 28-Dec-16 3:07am    
"en" is the same as "en-US" which means year/month/day to which your value of "29/12/2016" does not fit. Instead, it fits into "en-GB" which is for UK culture of day/month/year.
CPallini 28-Dec-16 2:59am    
5.
Peter Leow 28-Dec-16 3:10am    
Thank you, CPallini.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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