You will need to convert the value if you are passing dates back and forth. In javascript,
var d = new Date()
d.setTime(-62135575200000);
alert(d.toDateString());
See the question Converting .NET DateTime to JSON and related answers there.
The following shows the two ways commented upon to move the dates.
in my code behind:
[WebMethod]
public static DateTime loadDate()
{
return DateTime.Now;
}
[WebMethod]
public static double loadDateTicks()
{
return DateTime.Now.UnixTicks();
}
public static class ExtensionMethods
{
// returns the number of milliseconds since Jan 1, 1970 (useful for converting C# dates to JS dates)
public static double UnixTicks(this DateTime dt)
{
DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = dt.ToUniversalTime();
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
return ts.TotalMilliseconds;
}
}
All credit for that extension method goes to "Jeff Meatball Yang".
My front end test is the following:
function LoadDates() {
$.ajax({
url: "Default.aspx/loadDate",
type: "POST",
data: "{}",
contentType: "application/json; charset=utf-8",
success: function (msg) {
var re = /-?\d+/;
var d = new Date(parseInt(re.exec(msg.d)[0]));
alert(d.toDateString());
},
dataType: "json"
});
$.ajax({
url: "Default.aspx/loadDateTicks",
type: "POST",
data: "{}",
contentType: "application/json; charset=utf-8",
success: function (msg) {
var dt = new Date(msg.d);
alert(dt.toDateString());
},
dataType: "json"
});
}