I would like to convert a Javascript date format to ASP.NET date format.
2012-09-10 12:00PM to /Date(1347442050050-0700)/
Because I'm passing it back to the server. I got the ASP.NET format from the request I did on the server, then convert it to Javascript date using moment.js:
moment("/Date(1347442050050-0700)/").format("YYYY-MM-DD hh:mmA");
Is there a way to do this?
I would like to convert a Javascript date format to ASP.NET date format.
2012-09-10 12:00PM to /Date(1347442050050-0700)/
Because I'm passing it back to the server. I got the ASP.NET format from the request I did on the server, then convert it to Javascript date using moment.js:
moment("/Date(1347442050050-0700)/").format("YYYY-MM-DD hh:mmA");
Is there a way to do this?
Share Improve this question edited Sep 24, 2012 at 3:01 Jon Adams 25.1k18 gold badges84 silver badges121 bronze badges asked Sep 12, 2012 at 9:38 n0minaln0minal 3,2239 gold badges49 silver badges72 bronze badges 1- Do you want asp => js or js => asp? – mornaner Commented Sep 12, 2012 at 10:08
3 Answers
Reset to default 8I got what i need. If this is somehow wrong please ment.
var test = moment("2012-09-10 12:00PM").valueOf();
var test2 = moment("2012-09-10 12:00PM").format("ZZ");
var test1 = "/Date("+test+test2+")/";
alert( test1 ); // returns /Date(1347206400000+0800)/
var string = moment(test1).format("YYYY-MM-DD hh:mmA");
alert( string ); // returns 2012-09-10 12:00PM
You can add the function to the moment prototype so that it's a little more portable.
http://jsfiddle/timrwood/qe8pk/
moment.fn.toASP = function () {
return '/Date(' + (+this) + this.format('ZZ') + ')';
}
If you want to send a date back to an ASP.NET ASMX web service where the RPC method receives a DateTime object, this may be helpful: https://stackoverflow./a/12973157/1145963