daily pastebin goal
32%
SHARE
TWEET

Dotnet DateTime to SqlDateTime interval

a guest Dec 9th, 2011 161 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static class DateTimeExtensions
  2.     {
  3.         /// <summary>This method returns the datetime fitting into min/max of Sqlserver
  4.         /// as per http://codebetter.com/petervanooijen/2008/08/21/valid-date-time-values-in-sql-server-sqldatetime-vs-datetime/
  5.         /// More info can be found at http://www.selfelected.com/convert-dotnet-datetime-to-sqlserver-sqldatetime/
  6.         /// </summary>
  7.         /// <param name="dt"></param>
  8.         /// <returns></returns>
  9.         public static DateTime ToValidSqlserverDatetime(this DateTime dt)
  10.         {
  11.             //  Must cast System.Data.SqlTypes.SqlDateTime.MinValue: http://codebetter.com/petervanooijen/2008/08/21/valid-date-time-values-in-sql-server-sqldatetime-vs-datetime/
  12.             if (dt < (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue)
  13.             {
  14.                 return (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
  15.             }
  16.             else if (dt > (DateTime)System.Data.SqlTypes.SqlDateTime.MaxValue)
  17.             {
  18.                 return (DateTime)System.Data.SqlTypes.SqlDateTime.MaxValue;
  19.             }
  20.             else
  21.             {
  22.                 return dt;
  23.             }
  24.         }
  25.     }
  26.  
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top