Thursday, September 17, 2015

[JavaScript] SQLServer Date to jsDateTime

I forgot to include in the last posting the reverse function that takes a SQLServer Epoch value and converts it to a JavaScript datetime number.
function SQLServerDateTojsDateTime(a) {
  return Math.ceil((a * 86400 - 2208988800) * 1000);
 }
An example of its use (from a jscli session):
> d = new Date()
 Thu Sep 17 12:03:58 UTC+0800 2015
 > d.valueOf()
 1442462638627
 > n = jsDateTimeToSQLServerDateTime(d)
 42262.169428553236
 > m = SQLServerDateTojsDateTime(n)
 1442462638627
 > new Date(m)
 Thu Sep 17 12:03:58 UTC+0800 2015
Enjoy!

© Copyright Bruce M. Axtens, 2015

No comments: