Converting SQL Server datatime format to unix time
We are working with a JSON API that returns times in what we have been told is the SQL Server 开发者_StackOverflow社区date time format, for example: /Date(-6847804800000+0000)
I'm trying to convert this into unix time to use standard methods on it, does anyone know an easy way to do this? If not, converting it into any sort of other date format would be helpful.
PS: We don't have access to the SQL Server database to change the way the API spits the data out.
Quick check with PowerShell:
PS> [datetime]"1970-01-01 00:00" + [timespan]::FromMilliseconds(-6847804800000)
01 January 1753 00:00:00
So commenters Martin and Gerben appear to be correct. This is millisecond offset from 19700101T000000.
The service is passing out SqlDateTime.MinValue
+ the hours of the day it is talking about.
The base of 1753 is used to determine an offset value, your offset is 1970 instead.
Firstly, you want to take the returned -6847804800000 and add the total miliseconds between 1753 and 1970. This will give you the offset in from 1970.
SqlDateTime minSqlDateTime = SqlDateTime.MinValue;
DateTime minTimeStampDateTime = new DateTime(1970, 1, 1);
double milli = (minTimeStampDateTime - (DateTime)minSqlDateTime).TotalMilliseconds; // = 6847804800000
double toDetermineTicks = -6847804080000; // so this is 01/01/1753 12:00 PM
double ActualUnixTicks = (toDetermineTicks + milli) / 1000;
精彩评论