473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

DateTime and localization

I have a string representing the date in US format (mm/dd/yyyy). I use
Convert.ToDateTime() to convert this to a DateTime structure. I then compare
this to the current date DateTime.ToDay.

All works fine until the localization is changed, for example to Germany,
then the string representing the date is incorrectly interpreted as if it
were in the local format dd/mm/yyyy (or is it dd-mm-yyyy) with a resulting
incorrect value for the DateTime with a resultant invalid comparison to the
current date.

Any suggestions on how to handle this would be appreciated.

Is there some way that I can specify that my string should be converted to a
DateTime structure using the “en_US” culture?

Thankyou.

Nov 26 '07 #1
2 1397
>Is there some way that I can specify that my string should be converted to a
>DateTime structure using the en_US culture?
Use one of the ToString overloads that let you supply a
DateTimeFormatInfo so you can set the format.

Dave
Nov 27 '07 #2
I have a string representing the date in US format (mm/dd/yyyy). I use
Convert.ToDateTime() to convert this to a DateTime structure. I then
compare this to the current date DateTime.ToDay.
General rule: all processing should happen on locale-insensitive data,
all user-visible items should be locale-sensitive.

If the string comes from from user, it should be converted to a structure
using the user preferences (CurentCulture) and DateTime.TryParse or
DateTime.TryParseExact.
If the string comes from another module, then you can either use a
CultureInvariant, or pass a your format to TryParseExact.

The current format (mm/dd/yyyy) strikes me as "very American,"
and if you have control on the module generating that I woul recoment
moving to the iso format (yyyy.mm.dd), which is more "locale-independent"

See also here: http://www.mihai-nita.net/article.php?artID=20051025a
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Nov 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rental | last post by:
I'm having the sam problem as described below with the Localization toolkit. Does anyone know if there is a solution to this problem. --->When attempting to generate resource dlls with...
2
by: Tronex | last post by:
Hi there, I am desperate and need help on this from someone who actually knows what he does... cuz I certainly don't ;-) This is the situation: I have an ASP.NET web application (written in...
5
by: Michel Posseth [MCP] | last post by:
Hello we have encountered the following problems with the date time picker control A : datetime picker control gives focus to last entered field when moving back and forward with focus how do...
2
by: LukasMalik | last post by:
Hi all, in my application, I am using DateTime. Until I got in contact with SQL I did not try to solve this problem. Where is set DateTime format? Will somehow change application DateTime format...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.