views:

52

answers:

2

Hi,

I am using the .NET NewtonSoft JSON serialization library and it is expecting date fields in this format:

"UpdateTimestamp":"\/Date(1280408171537+0100)\/"

Does anyone know how I can format javascript date object into this format?

Thanks

Paul

A: 

try this:

var UpdateTimestamp = ""\/Date(" + (new Date().getTime()) + "+0100)\/";
jcubic
Looks reasonable, but I'm guessing the `"+0100"` is the time zone offset.
Matt Ball
In fact: http://james.newtonking.com/projects/json/help/
Matt Ball
Insted +0100 you can use (new Date().getTimezoneOffset())
jcubic
A: 

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.

cm2