dates day and month problem

dashley

Well-known member
Joined
May 27, 2005
Messages
59
Location
Tennessee
Programming Experience
10+
vb.net 2005 pocket pc 2003
The statement below works fine.

VB.NET:
Dim s As String = "8/12/2006"
Dim d As Date = Date.Parse(s)
MsgBox(d.ToShortDateString)

If I change the date to "8/13/2006"

I get an error. For some reason its looking at the first sample date as 8 -Dec -2006 where it should be August 12 2006.

Its reading all my dates as MM/DD/YYYY. Even if i format the dates (dd/MM/yyyy) it filps them.

Is this normal for a device? I have never run into it boefore and its driving me nuts



 
Disreagard.

after hours of fighting this I found that the device itself had a different date format chosen (dd/mm/yyyy). Once I reset it to (mm/dd/yyyy) everything started working.
 
Instead of changing the culture of the device you should change your code. DateTime.ParseExact allows you to specify expected date formats. Both DateTime.Parse and ParseExact also allow you to relate in code to whichever culture the device is set. It depends on the input date strings and who created them, but changing the devices locale/culture setting to fit the application is generally the worst solution imaginable. There could perhaps be a small excuse if this application is only for your own device and you had it wrongly configured, but your should still consider writing code that will dynamically adapt to different cultures.
 
John,

Thank you very much.

Actually I created the input strings as mm/dd/yyyy (a web service) . The code ran fine on the emulator. When l I deployed it to the test device (received form a US university) which had the wrong (dd/mm/yyyy) settings on it I started receiving the error. Everything Im doing is for US culture.

I'm totaly agree with you on writing the code dynamically and really appreciate the advice.

If you can will you point me in the right direction on how to query the device to determine what culture its set to. From there I can apply the datetime.parseexact method you presented.


Thanks Again

Dan
 
In that case where dates are returned from a US culture you must in clients set the DateTime to interpret these date as US culture. The documentation is the best place to start, it is pretty thorough both for explaining and giving code examples, DateTime structure http://msdn2.microsoft.com/en-us/library/system.datetime.aspx go to its methods, check the second Parse overloaded method, the example there is to read a freanch culture date string.
 
Back
Top