tags:

views:

340

answers:

4

I just found that my SqlDateTime.MinValue is different on different machines.

Sometimes it is = DateTime(1953, 1, 1, 0, 0, 0);

but on other machines it is = DateTime(1753, 1, 1, 0, 0, 0);

How can that be? OS on both machines is WinXP SP2.

Edit:

My code was like this:

DateTime date;

...

if (date == SqlDateTime.MinValue)
{
    return "(any)";
}
else
{
    return date.ToString();
} 

This code should never return January 1, 1753 but it did (on some machines). The minutes and seconds of date are not used by my code and should always remain default values.

A: 

Are you sure you're correct about the different values? SqlDateTime is documented as having a fixed start date at MSDN for all versions of the .NET Framework:

The minimum valid date for a SqlDateTime structure is January 1, 1753.

If it's genuinely reproducible then it sounds like it would be a bug in the .NET Framework (or the documentation) so Microsoft would be the people to ask for help. Support calls are free if the issue is found to be a bug.

Greg Beech
+1  A: 

I know this issue happened on old Windows NT machines, where the date range was 1953-XXXX, instead of 1753-9999. Are you totally sure that these machines are both running Windows XP?

Jan Jongboom
A: 

As others have said, SqlDateTime static constructor sets the year. So unless you are calling another constructor there's no obvious explanation.

static SqlDateTime()
{
    ...
    MinYear = 1753;
    ...
}

Reflector shows it as 0x6d9

Chris S
A: 

On a slight tangent, but as you are using SqlDateTime.MinValue as a special value, I'd suggest you use a nullable DateTime instead like this:

DateTime? date = null;
...
if (!date.HasValue)
{
    return "(any)";
}
else
{
    return date.Value.ToString();
} 

Note the shorthand for a nullable type is using the question mark: e.g. DateTime?

As I said in my comment, I wonder whether there is a cast to string that is making it appear that SqlDateTime.MinValue is returning a difference value, when in fact it's the formatting to string that is actually the problem (e.g. regional settings).

AdaTheDev