Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class DateTimeExtensions
- {
- /// <summary>This method returns the datetime fitting into min/max of Sqlserver
- /// as per http://codebetter.com/petervanooijen/2008/08/21/valid-date-time-values-in-sql-server-sqldatetime-vs-datetime/
- /// More info can be found at http://www.selfelected.com/convert-dotnet-datetime-to-sqlserver-sqldatetime/
- /// </summary>
- /// <param name="dt"></param>
- /// <returns></returns>
- public static DateTime ToValidSqlserverDatetime(this DateTime dt)
- {
- // Must cast System.Data.SqlTypes.SqlDateTime.MinValue: http://codebetter.com/petervanooijen/2008/08/21/valid-date-time-values-in-sql-server-sqldatetime-vs-datetime/
- if (dt < (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue)
- {
- return (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
- }
- else if (dt > (DateTime)System.Data.SqlTypes.SqlDateTime.MaxValue)
- {
- return (DateTime)System.Data.SqlTypes.SqlDateTime.MaxValue;
- }
- else
- {
- return dt;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement