469,609 Members | 1,687 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

Converting String to DateTime not working

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?
Nov 15 '05 #1
5 8588
Cameron <co*********@hotmail.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2


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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
Cameron <an*******@devdex.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4


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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #5
Cameron <an*******@devdex.com> wrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Marcio Kleemann | last post: by
4 posts views Thread by M. Uppal | last post: by
8 posts views Thread by Mika M | last post: by
3 posts views Thread by NateM | last post: by
6 posts views Thread by B.N.Prabhu | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.