Click here to Skip to main content
13,629,209 members
Rate this:
Please Sign up or sign in to vote.
See more:

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>

Posted 27-Dec-16 19:55pm
Updated 27-Dec-16 20:11pm

1 solution

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

Solution 1

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

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


Maciej Los 28-Dec-16 2:33am
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
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 | Cookies | Terms of Service
Web02 | 2.8.180712.1 | Last Updated 28 Dec 2016
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

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