4

We are hosting a website that when browsed to locally loads up fine but if you browse to it externally you get

Conversion from string "1/15/2010 8:46:01 AM" to type 'Date' is not valid.

What do I change on the server to fix this. When browsing the site on the server it works, although I am logged into the server as a different user to that in IIS.

Any ideas this is actually starting to cause a head ache now.

Massimo
  • 68,714
  • 56
  • 196
  • 319
  • 4
    Show the code that is Parsing/Converting the string to DateTime –  Jan 15 '10 at 13:25
  • is it possible that it doesn't like the 1 (instead of 01) Also, is the code that makes the conversion to date client-side or server-side? –  Jan 15 '10 at 13:27
  • I'm very sorry we don't have the source for the site so I can give you no code. I can only tell you the site did work and something has changed and I can't find it! –  Jan 15 '10 at 13:31
  • You can have a look at .Net Reflector to check the code out. http://www.red-gate.com/products/reflector/ –  Jan 15 '10 at 13:37
  • Does the site use different user accounts between when it's browsed to locally vs. externally? If so, then the likely culprit is different regional settings between the two accounts. – John Saunders Jan 15 '10 at 13:42
  • Since Robert mentions "We have no source and can't change it. I just need to find the OS setting that will allow this to work again", this is probably better suited for serverfault –  Jan 15 '10 at 13:42

7 Answers7

1

It's probably to do with the Locale you're using with the conversion. Maybe externally the UK format is being used, but internally the US format is used. Make sure you specify the exact format you're expecting when converting the string to a date.

Graham Clark
  • 111
  • 4
  • We have no source and can't change it. I just need to find the OS setting that will allow this to work again. –  Jan 15 '10 at 13:32
1

I would like to see the code that's giving you trouble, but "1/15/2010 8:46:01 AM" is a valid date in en-US encoding. Make sure you are using the en-US culture; don't assume it based on the machine you are using for development/test. Also, if DateTime strings are based on input, consider the cases where the user can override the current culture via the Control Panel.

Here's an example from MSDN:

string dateString;
CultureInfo culture;
DateTimeStyles styles;
DateTime dateResult;

// Parse a date and time with no styles.
dateString = "03/01/2009 10:00 AM";
culture = CultureInfo.CreateSpecificCulture("en-US");
styles = DateTimeStyles.None;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
   Console.WriteLine("{0} converted to {1} {2}.", 
                     dateString, dateResult, dateResult.Kind);
else
   Console.WriteLine("Unable to convert {0} to a date and time.", 
                     dateString);
1

It seems like the OS change you'd need is to go in to control panel > Regional & Language Options > Standards & Formats > Set the Language dropdown to English (UK) and Location to United Kingdom.

Ideally, code should use Culture Invariant Dates to get around these issues along with date formatting methods.

Tanner
  • 111
  • 3
1

Since you mention in a comment on one of the other answers that from some machines it works, while on others it doesn't, that suggests the problem might be on the client.

When browsing the web, your browser provides the server with a set of accepted/prefered locales.

You should take a look at these settings on the machines where things work, and compare them to the settings on the machines where it doesn't work. Could be some machines have been switched from requesting a UK locale to requesting a US locale (or the other way around).

Epcylon
  • 217
  • 4
  • 10
1

Source or no source, if it's an ASP.NET application you can change the culture in the web.config to the correct one - see this question and the comment to my answer.

<system.web>
 <globalization culture="en-US" uiCulture="en-US" />
<system.web />

As the OS setting is per-user you will have some problems trying to change it using the OS control panel - hence trying to change it in the application as above is usually easier and more robust.

Oskar Duveborn
  • 10,740
  • 3
  • 32
  • 48
0

The locale of the server or database has been set to US (as month/day/year is a US format date), but the code is expecting a UK format date (day/month/year).

Check these and make sure they match and it should be OK.

ChrisF
  • 1,861
  • 1
  • 21
  • 28
0

Take a look at How to Change the Default Server Locale
You could try this setlocale.exe file to set it

00000409 for English (US).
00000809 for English (UK).

Complete list here

SwDevMan81
  • 201
  • 1
  • 3
  • 14
  • I'll take this answer as it definitely would help. The problem has become more 'odd' since earlier. It works from some machines and not others :). –  Jan 15 '10 at 15:40
  • 1
    @Robert If this is the case then the problem appears to be that the server code does not adequately handle dates from other locales - that may not be a problem you can solve on the Server OS itself, have you asked whoever owns the code if it handles international clients? – Helvick Jan 17 '10 at 20:20