{"id":148,"date":"2010-12-04T14:20:10","date_gmt":"2010-12-04T13:20:10","guid":{"rendered":"http:\/\/thedifficultbit.com\/?p=148"},"modified":"2018-12-24T14:59:23","modified_gmt":"2018-12-24T13:59:23","slug":"rounding-net-datetime-to-match-sql-server","status":"publish","type":"post","link":"https:\/\/thedifficultbit.com\/?p=148","title":{"rendered":"Rounding .Net DateTime to match SQL Server"},"content":{"rendered":"\n<p>The  SQL Server DateTime type is represented by LINQ to SQL as a .Net System.DateTime type. This presents a small problem in that the .Net type supports more precision &#8211; a &#8220;tick&#8221; representing 100 nanoseconds &#8211; than the SQL type which is &#8220;rounded to increments of .000, .003, or .007 seconds&#8221;.<\/p>\n\n\n\n<p>It is worth noting that the actual <a href=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20050902-00\/?p=34333\">accuracy<\/a> of the .Net date is likely to be less than 100 nanoseconds.<\/p>\n\n\n\n<p>This difference in accuracy is not normally a problem but it can be difficult in testing when you might wish to write a value to the database and then read it to check it has been saved correctly: a  DateTime will likely not be the same as the value saved as it will have been rounded.<\/p>\n\n\n\n<p>The function below is an attempt to replicate the rounding performed by SQL Server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\/ &lt;summary>\n\/\/\/ Rounds a DateTime as it would be changed by SQL.\n\/\/\/ &lt;\/summary>\n\/\/\/ &lt;remarks>\n\/\/\/ The .Net DateTime is accurate to 0.1 milliseconds (100 nanoseconds). SQL Server DateTime is \"Rounded to increments\n\/\/\/ of .000, .003, or .007 seconds\" i.e. 0, 3 or 7 milliseconds.\n\/\/\/ &lt;\/remarks>\npublic static DateTime DateTimeAsFromSql(DateTime dateTime)\n{\n\tDateTime rounded = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second);\n\tlong ticksDifference = dateTime.Ticks - rounded.Ticks;\n\tint milliseconds = ((int)(ticksDifference \/ 100000)) * 10;\n\tlong ticks = ticksDifference - (milliseconds * 10000);\n\tif (ticks >= 0 &amp;&amp; ticks &lt; 17000)\n\t{\n\t\tmilliseconds += 0;\n\t}\n\telse if (ticks >= 17000 &amp;&amp; ticks &lt; 50000)\n\t{\n\t\tmilliseconds += 3;\n\t}\n\telse if (ticks >= 50000 &amp;&amp; ticks &lt; 84000)\n\t{\n\t\tmilliseconds += 7;\n\t}\n\telse if (ticks >= 84000 &amp;&amp; ticks &lt;= 99999)\n\t{\n\t\tmilliseconds += 10;\n\t}\n\telse\n\t{\n\t\tthrow new InvalidOperationException(\"ticks: \" + ticks);\n\t}\n\n\treturn rounded.AddMilliseconds(milliseconds);\n}\n<\/code><\/pre>\n\n\n\n<p>Of course, a better solution is to use one of the other SQL date formats that allow for more precision.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The SQL Server DateTime type is represented by LINQ to SQL as a .Net System.DateTime type. This presents a small problem in that the .Net type supports more precision &#8211; a &#8220;tick&#8221; representing 100 nanoseconds &#8211; than the SQL type which is &#8220;rounded to increments of .000, .003, or .007 seconds&#8221;. It is worth noting [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2,12],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-net","tag-sql-server"],"_links":{"self":[{"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=148"}],"version-history":[{"count":4,"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":152,"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=\/wp\/v2\/posts\/148\/revisions\/152"}],"wp:attachment":[{"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thedifficultbit.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}