Round .NET DateTime milliseconds, so it can fit SQL Server milliseconds

前端 未结 4 1481
孤独总比滥情好
孤独总比滥情好 2020-12-03 10:59

I want to convert the datetime value to the value that I will get from SQL Server 2008.

SQL Server truncate the milliseconds to 3 digits, so I truncate the milliseco

4条回答
  •  庸人自扰
    2020-12-03 11:47

    Recommend building upon @RobSiklos solution since use of SqlDateTime in this fashion results in the loss of timezone information that the 'date' argument provided. Find its best practice to ensure timezone info is consistent at the point of conversion by adding a call to DateTime.SpecifyKind:

    using System.Data.SqlTypes; // from System.Data.dll
    
    public static DateTime RoundToSqlDateTime(DateTime date)
    {
      return DateTime.SpecifyKind( new SqlDateTime(date).Value, date.Kind);
    }
    

提交回复
热议问题