From another answer on Stackoverflow is a conversion from Javascript date to .net DateTime:
long msSinceEpoch = 1260402952906; // Value from Date.getTime() i
This method is working for me:
public sCdateToJsDate(cSDate: any): Date {
// cSDate is '2017-01-24T14:14:55.807'
var datestr = cSDate.toString();
var dateAr = datestr.split('-');
var year = parseInt(dateAr[0]);
var month = parseInt(dateAr[1])-1;
var day = parseInt(dateAr[2].substring(0, dateAr[2].indexOf("T")));
var timestring = dateAr[2].substring(dateAr[2].indexOf("T") + 1);
var timeAr = timestring.split(":");
var hour = parseInt(timeAr[0]);
var min = parseInt(timeAr[1]);
var sek = parseInt(timeAr[2]);
var date = new Date(year, month, day, hour, min, sek, 0);
return date;
}
This should do the trick:
date.Subtract(new DateTime(1970, 1,1)).TotalMilliseconds
I know this is a little late, but here's the solution I had to come up with for handling dates when you want to be timezone independent. Essentially it involves converting everything to UTC.
From Javascript to Server:
Send out dates as epoch values with the timezone offset removed.
var d = new Date(2015,0,1) // Jan 1, 2015
// Ajax Request to server ...
$.ajax({
url: '/target',
params: { date: d.getTime() - (d.getTimezoneOffset() * 60 * 1000) }
});
The server then recieves 1420070400000 as the date epoch.
On the Server side, convert that epoch value to a datetime object:
DateTime d = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(epoch);
At this point the date is just the date/time provided by the user as they provided it. Effectively it is UTC.
Going the other way:
When the server pulls data from the database, presumably in UTC, get the difference as an epoch (making sure that both date objects are either local or UTC):
long ms = (long)utcDate.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
or
long ms = (long)localDate.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local)).TotalMilliseconds;
When javascript receives this value, create a new date object. However, this date object is going to be assumed local time, so you need to offset it by the current timezone:
var epochValue = 1420070400000 // value pulled from server.
var utcDateVal = new Date(epochValue);
var actualDate = new Date(utcDateVal.getTime() + (utcDateVal.getTimezoneOffset() * 60 * 1000))
console.log(utcDateVal); // Wed Dec 31 2014 19:00:00 GMT-0500 (Eastern Standard Time)
console.log(actualDate); // Thu Jan 01 2015 00:00:00 GMT-0500 (Eastern Standard Time)
As far as I know, this should work for any time zone where you need to display dates that are timezone independent.
Another late answer, but this is missing here. If you want to handle conversion of serialized /Date(1425408717000)/
in javascript, you can simply call:
var cSharpDate = "/Date(1425408717000)/"
var jsDate = new Date(parseInt(cSharpDate.replace(/[^0-9 +]/g, '')));
Source: amirsahib