Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would expect the following to write a 2030 date into targetDate. It doesn't. It gives a 1930 date.

VB
Dim ci As CultureInfo = CurrentThread.CurrentCulture.Clone
ci.Calendar.TwoDigitYearMax = 2099
DateTime.TryParse("12/8/30", ci, DateTimeStyles.AssumeLocal, targetDate)

However if I set current culture to the clone then I get the expected (and required) 2030 date. But only if the code is running in a console app (Framework 2 or Framework 4.5 on Win7 Professional).

VB
Dim ci As CultureInfo = CurrentThread.CurrentCulture.Clone
ci.Calendar.TwoDigitYearMax = 2099

CurrentThread.CurrentCulture = ci
DateTime.TryParse("12/8/30", ci, DateTimeStyles.AssumeLocal, targetDate)

If the second fragment is run server side in a web app (Framework 2, Win7 Professional) then targetDate is _always_ a 20th century (19xx) date. This not desired behaviour and seems to
contradict the documentation.

Both the console apps. and the Web app. are run on the same (development) machine. Culture settings are not altered between runs.

Anyone else seen this?
Have you an explanation for what's going on?
Posted
Updated 24-Jun-13 4:21am
v3
Comments
lewax00 24-Jun-13 10:07am    
Hmm...I guess the first thing to check here is: does CurrentThread.CurrentCulture return the same value in both cases? Could be coincidence that it worked in the first place.

1 solution

You probably also want to set the TwoDigitYearMax on the CurrentUICulture property of the CurrentThread. For some reason there are two culture objects, one for background threads, and one for UI threads. You only seem to be setting it on the background thread one and not the UI one.

It works in the console because there is no UI, so it uses CurrentThread.CurrentCulture.

EDIT---

If you want to add comments, please use the "Have a question or comment?" link below the solution :)

[cigwork note]
Quote:
Thanks. That works, had tried setting UICulture on its own, but tinkering seems to show that you need to set both culture properties _and_ hand in a cultureinfo instance to the TryParse call.
 
Share this answer
 
v2

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