Click here to Skip to main content
15,881,776 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
Hey there community,
I have one question why my code is working. :P
Yeah, sounds strange, but I'm just curious...

When I'm starting my application in English I'm setting my current culture info:
C#
CultureInfo cultureInfo;
cultureInfo = new CultureInfo("en-US");
cultureInfo.DateTimeFormat.ShortDatePattern = "MM/dd/yy";
cultureInfo.DateTimeFormat.LongDatePattern = "MM/dd/yyyy";
cultureInfo.DateTimeFormat.ShortTimePattern = "HH:mm";
cultureInfo.DateTimeFormat.LongTimePattern = "HH:mm:ss";
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo


Looking in the MSDN of DateTime.Parse() [^], I find the following description:
"Parse a date and time string by using the conventions of the current culture. --> Parse(String) overload"

But the following Parse (with our Austrian date format) works without problems:
C#
DateTime toCompare = DateTime.Parse("01.01.1900 00:00:00");


Can anyone explain why i don't have to use the DateTime.Parse(string, IFormatProvider) overload?
As far as I understand the MSDN, my current .Parse() should not work...

Thanks, Markus
Posted
Comments
NeonMika 25-Sep-13 7:19am    
Okay, some extra input, just found the following (this wasn't mentioned in the german MSDN article)

[...]If you parse a date and time string by using a DateTimeFormatInfo object with customized settings that are different from those of a standard culture, use the ParseExact method instead of the Parse method to improve the chances for a successful conversion. A non-standard date and time string can be complicated and difficult to parse. The Parse method tries to parse a string with several implicit parse patterns, all of which might fail. In contrast, the ParseExact method requires you to explicitly designate one or more exact parse patterns that are likely to succeed.[...]

Could this have to do with my case?
BillWoodruff 25-Sep-13 9:44am    
good question. upvoted.
member 8888995 26-Sep-13 0:39am    
Hi ,
Please check your systems Date, Time, Language, and Regional Options Settings.

1 solution

The forward slash "/" is not meant to be a forward slash literally - it stands for the separator used in the date format. You did not change that (you could do so with cultureInfo.DateTimeFormat.DateSeparator = "/";, consequently the dot "." is still acceptable as the separator. And by the way, the first 01 became January, the second 01 the first day of that month...
 
Share this answer
 

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