Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2007

Tip: Looking for answers? Try searching our database.

DateTime and localization

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
akiecker - 26 Nov 2007 20:57 GMT
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.
David Lowndes - 27 Nov 2007 00:03 GMT
>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
Mihai N. - 27 Nov 2007 02:00 GMT
> 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

Signature

Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.