Converting String to DateTime not working

  • Thread starter Thread starter Cameron
  • Start date Start date
C

Cameron

I accept a date from a user in a textbox, but I need to convert it to a DateTime.

I have the following code:

return DateTime.ParseExact("01/10/2003", "dd/MM/yyyy", new CultureInfo("en-GB"));

When I run it on my development PC it's fine - I get 1st October 2003.

When I run it from my webserver it doesn't work - I get 10th January 2003.

I don't understand why - can anyone help?
 
Cameron said:
I accept a date from a user in a textbox, but I need to convert it to a DateTime.

I have the following code:

return DateTime.ParseExact("01/10/2003", "dd/MM/yyyy", new CultureInfo("en-GB"));

When I run it on my development PC it's fine - I get 1st October 2003.

When I run it from my webserver it doesn't work - I get 10th January 2003.

How have you checked that you get 10th January 2003? Might it be that
you're then formatting it using a different culture? How are you
formatting it, in fact?
 
Thanks for the reply.

I'm just using the following to put the date back as a string:

myDate.ToString("dd/MM/yyyy")

and the date/month are reversed from the values they entered.

Regards,
Cameron
 
Cameron said:
Thanks for the reply.

I'm just using the following to put the date back as a string:

myDate.ToString("dd/MM/yyyy")

and the date/month are reversed from the values they entered.

Hmm... that should be okay.

I can't see how that could all go wrong, to be honest. What locale is
your web server in, compared with your desktop? I can't see how it
would affect things if you're using an explicit format like the above,
but...
 
When I log onto the web server it's in the same locale as my local PC,
but usually nobody is logged into it. It just sits there with the logon
screen. Aren't locale settings stored for each user? In this case I
don't know where it takes its locale settings from when nobody is logged
on.

But I wouldn't have thought locale settings mattered when I'm telling it
the exact date formats I'm providing.

It's annoying! All my reports run for the wrong date ranges.
 
Cameron said:
When I log onto the web server it's in the same locale as my local PC,
but usually nobody is logged into it. It just sits there with the logon
screen. Aren't locale settings stored for each user? In this case I
don't know where it takes its locale settings from when nobody is logged
on.

But I wouldn't have thought locale settings mattered when I'm telling it
the exact date formats I'm providing.

It's annoying! All my reports run for the wrong date ranges.

Locale settings are provided on a per user basis, but there's also a
system default. To be honest, this is a hairy topic - we've got a PC in
our office which we can't find *anything* suggesting it should be in
en_US, but it still is for some services :(

As you say, when you're specifying the exact date formats, it should be
fine though. Do you have a small sample ASP.NET page which is failing?

(As small as possible would be helpful - it could just parse and then
reformat a date which is hard-coded.)
 
Back
Top