Click here to Skip to main content
13,141,637 members (59,032 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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 27-Dec-16 19:55pm
viprat702
Updated 27-Dec-16 20:11pm
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

According to DateTime.Parse Method (String, IFormatProvider) (System)[^], the second parameter is IFormatProvider[^].
Try
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);
 
	}
}
  Permalink  
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


Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 28 Dec 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100