Formatting a JavaScript date into a string for JSON DeSeriailization
I am using the .NET NewtonSoft JSON serialization library and it is expecting date fields in this format:
"UpdateTimestamp":"\/Date开发者_C百科(1280408171537+0100)\/"
Does anyone know how I can format javascript date object into this format?
try this:
var UpdateTimestamp = ""\/Date(" + (new Date().getTime()) + "+0100)\/";
The format looks like unix time. You can get this using the valueOf method of the Date object. I imagine the part after the + sign is the timezone offset. You can get that with the getTimezoneOffset method.
For your particular application, you could something like this, prototyped onto the Date object:
Date.prototype.getTimestamp=function(){
var to = this.getTimezoneOffset()/60;
to = (to < 10) ? "0"+to: to;
return this.valueOf() //get the unix time
+"+"+to+"00";
}
** I forgot about it but you could also use getTime, as jcubic mentioned.
精彩评论