6

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.

TalkingCode
  • 13,407
  • 27
  • 102
  • 147
  • How are you using the value? i.e. is there any type casting going on (e.g. to & from string) - if so, could it be down to regional settings affecting the casting? – AdaTheDev Jan 12 '10 at 11:17
  • dupe: http://stackoverflow.com/questions/805770/sqldatetime-minvalue-c-datetime-minvalue-why – Dhanapal Jan 12 '10 at 11:24
  • 4
    This is **not** a duplicate. That question relates to why SqlDateTime.MinValue != DateTime.MinValue, a different question – AdaTheDev Jan 12 '10 at 11:28
  • Yes the difference between the two is obvious when you try to put DateTime.MinValue into SQL Server, as it doesn't like dates before 1753 which was the date when the Gregorian calendar was adopted by Britain. – Chris S Jan 12 '10 at 13:01
  • @Chris S - Unless you're using the new DATETIME2 data type, which supports the same range as the DateTime struct. – Greg Beech Jan 12 '10 at 13:14

4 Answers4

1

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
  • 26,598
  • 9
  • 83
  • 120
0

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
  • 133,383
  • 43
  • 204
  • 250
0

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
  • 64,770
  • 52
  • 221
  • 239
0

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
  • 142,592
  • 28
  • 206
  • 200