views:

303

answers:

2

We've just moved to a new server and everything's being going well. However earlier I ran into a .NET error when it was trying to convert a string in the format "dd/mm/yyyy" to a date, now I changed the culture setting in the local web.config but we require this to be global.

Where do I change this? I assumed the machine.config but I couldn't find any familiar settings on the older server.

Or am should I be looking at the server side and finding a setting to set the culture on the machine to be en-GB?

== UPDATE == I've changed the regional settings and it still fails unfortunately. Is there a machine.config global setting that may be screwing it up? At the moment it seems to be using invariant and failing with that.

I'm using Windows Server 2008 with IIS7 if that is any help at all.

The string being used is hard coded into the page because it is a closing date for a registration form for a one off site.

The code being ran is as follows:

if (DateTime.Now > Convert.ToDateTime("26/04/2008 12:00"))

Very simple and has worked a million times before now, it is only on this new setup that this error occurs.

+1  A: 

When migrating servers, I would recommend that you use the same Regional Settings (Control Panel) as before - if that is possible. Just doing that, you avoid a lot of problems.

That might not be possible though if the new server is going to host other applications that expect other regional settings or if you have to due that because of new policies.

In any case, it is a best practice to explicitly specify where applicable the format during all Parse and ToString() calls.

There's even a FxCop (and Static Analysis) rule for that.


Updated to reflect new information on the question:

Convert.ToDateTime is a "convenience" method is implemented as:


public static DateTime ToDateTime(string value)
{
    if (value == null)
    {
        return new DateTime(0L);
    }
    return DateTime.Parse(value, CultureInfo.CurrentCulture);
}

Note that it calls DateTime.Parse passing an explicit CultureInfo (CultureInfo.CurrentCulture).

CultureInfo.CurrentCulture gets it value from Thread.CurrentThread.CurrentCulture.

You can take a look at CultureInfo.CurrentCulture to see what CultureInfo it is actually returning and/or investigate further to try to understand what is happening, but on the other hand, it is recommended that you explicitly specify the formatter to be used when parsing strings into other data types such as DateTime, Double, etc.

As I said in my original answer, there is an FxCop (Static Analysis) rule to check for the lack of an IFormatProvider passed to methods that have overloads receiving an instance of it: CA1305.

So I would recommend you rewrite the code something along the lines:


if (DateTime.Now > DateTime.Parse("26/04/2008 12:00", new CultureInfo("en-GB")))
Alfred Myers
I've changed the regional settings and it still fails unfortunately.Is there a machine.config global setting that may be screwing it up? At the moment it seems to be using invariant.
John_
I appreciate the additional information and I will definately take it on board in the future. However it didn't actually solve my problem as I have about 30 websites that would need updating and not the time to do it.
John_
I voted up in recognition of the good answer.
John_
A: 

In the end (and I'm not sure if this was the best way of doing it or not) I had to select the computer from within IIS7 and set the globalization settings there to en-GB. This did resolve the issue and will also keep all my other websites in the future in that culture unless specified.

Note that I had already changed the computers regional settings to UK and that had not resolved the issue.

I will however from now one go forward with Alfred's suggestion of specifying the culture to parse the date as.

John_
This route worked for me, changing the locale for the machine required a restart.
CountZero
I did that too, I'm sure I've missed something I just can't find out what it is.
John_