Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SmallDateTime Min and Max Values in C#

Tags:

c#

sql

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?

like image 496
Magpie Avatar asked Nov 10 '10 17:11

Magpie


People also ask

What is the maximum date value that can be stored in a Smalldatetime?

smalldatetime description YYYY is four digits, ranging from 1900, to 2079, that represent a year.

What is Smalldatetime C#?

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'.

How do I write Smalldatetime in SQL?

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[.


2 Answers

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.

like image 55
George Stocker Avatar answered Oct 21 '22 08:10

George Stocker


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.

like image 41
Andomar Avatar answered Oct 21 '22 08:10

Andomar