In C# there's a SqlDateTime.MinValue
and SqlDateTime.MaxValue
, but I can't find one for the SmallDateTime
datatype from SQL Server.
var smallDateTimeMin = DateTime(1900, 1, 1);
var smallDateTimeMax = DateTime(2079, 6, 6);
Is there one or do I need to implement this myself?
smalldatetime description YYYY is four digits, ranging from 1900, to 2079, that represent a year.
smalldatetime is internally a 32-bit integer, containing a count of minutes since the smalldatetime epoch (1900-01-01 00:00). In conversion, seconds and fractional seconds are rounded using SQL Server's arcane date/time rounding rules, so the date 2013-01-31 23:59:59 gets rounded to the next date 2013-02-01 00:00:00'.
smalldatetime – format is YYYY-MM-DD hh:mm:ss; stores values from 1900-01-01 to 2079-06-06; with the accuracy of 1 minute; uses 4 bytes. datetime2 –format is YYYY-MM-DD hh:mm:ss[.
Why not use an extension method?
public static class DateTimeExtensions
{
public static DateTime SmallDateTimeMinValue(this DateTime sqlDateTime)
{
return new DateTime(1900, 01, 01, 00, 00, 00);
}
public static DateTime SmallDateTimeMaxValue(this DateTime sqlDateTime)
{
return new DateTime(2079, 06, 06, 23, 59, 00);
}
}
DateTime date = DateTime.Now;
Console.WriteLine("Minvalue is {0} ", date.SmallDateTimeMinValue().ToShortDateString());
Admittedly, it'd be nice for extension properties, but those don't exist yet.
There is no smalldatetime
equivalent in System.Data.SqlTypes
. Nor is there an equivalent for the new datetime2
type. So I wouldn't expect min-max constants for those types in the .NET framework.
But the types are well documented on MSDN:
Date and time data from January 1, 1753 through December 31, 9999, to an accuracy of one three-hundredth of a second (equivalent to 3.33 milliseconds or 0.00333 seconds). Values are rounded to increments of .000, .003, or .007 seconds, as shown in the table.
So you can easily define your own min-max constants.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With