Processing Date Input

G

Guest

Example:

On a web page the user enters for a date: 12/05/2006
They're in the UK, so this is 12th May 2006
Once this value arrives in a datetime variable in VB, date.month = 12 and
not 5 as should be expected, ie, it has assumed a US date formatting.

We have tried the following:

Setting both
System.Threading.Thread.CurrentThread.CurrentCulture
System.Threading.Thread.CurrentThread.CurrentUICulture

to "EN-GB"

And

Dim ukCulture As CultureInfo = New CultureInfo("en-GB")
Dim myDateTime As DateTime = DateTime.Parse(myDate, ukCulture.DateTimeFormat)

Neither of these approaches have resolved the fundamental issue (US format
in the resulting variable rather than a UK date (month - day swapped).

Also, .ToString versions of the variables have been passed around, using a
string to capture the variable before processing and so on.

Also, once you change the input date to something like 28/1/2006, the
processes fail to even accept it as a valid date format (obviously because 28
is greater than the maximum month of 12)

Control Panel on the development machine shows Uk/British settings in all
appropriate places.

Is there a way (and there surely must be?) to set ASP/VB.Net to use local
settings by default when processing this data? Or a genuine way of
converting between types that is actually reliable and doesn't require us to
make an assumption that the date is in x format so we can string strip and
rearrange.

Using Visual Studio 2005 Professional on XP Pro SP2, all known updates and
patches installed, with the environment being on this single machine.

Thanks
 
M

Mark Rae

On a web page the user enters for a date: 12/05/2006
They're in the UK, so this is 12th May 2006
Once this value arrives in a datetime variable in VB, date.month = 12 and
not 5 as should be expected, ie, it has assumed a US date formatting.
Is there a way (and there surely must be?) to set ASP/VB.Net to use local
settings by default when processing this data? Or a genuine way of
converting between types that is actually reliable and doesn't require us
to
make an assumption that the date is in x format so we can string strip and
rearrange.

There is only one 100% guaranteed way for dates to be completely unambiguous
across all cultures:

FORMAT THEM PROPERLY!!!

If you absolutely *MUST* allow users to enter dates manually (i.e. not
through some sort of date picker control or whatever), then you must
reformat the date as soon as it has been entered in the following format:

dd MMM yyyy

No matter where you are in the world 12 May 2006 cannot be misinterpreted as
anything else.

However, I strongly urge you to use a date picker - there are loads of them
on the web.
 
S

Saitir

That's great, and true.

However, it doesn't alter the basic point that the date conversion tools
don't convert between two well formatted date types as they're supposed to.

I'm all for work arounds and so on, but when the tools are supposed to do
something, I'd prefer them to.
 
S

S. Justin Gengo

Saitir,

I think that Mark has given you good advice. Using a date picker will make
the page work for anyone, anywhere, but if you want to you can localize it
for Great Brittain.

You are running into a conversion problem because by default the date is
coming into the system as a US date. You have to change what the page
considers to be the default format. This is done through localization.
Please see the following article Microsoft document, I think it will help:
http://msdn2.microsoft.com/en-us/library/fw69ke6f.aspx

Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top