Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Javascript Date to ASP.NET Date format

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?

like image 771
n0minal Avatar asked Sep 12 '12 09:09

n0minal


2 Answers

I got what i need. If this is somehow wrong please comment.

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
like image 64
n0minal Avatar answered Nov 01 '22 23:11

n0minal


You can add the function to the moment prototype so that it's a little more portable.

http://jsfiddle.net/timrwood/qe8pk/

moment.fn.toASP = function () {
    return '/Date(' + (+this) + this.format('ZZ') + ')';
}
like image 38
timrwood Avatar answered Nov 01 '22 23:11

timrwood