8

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?

1
  • Do you want asp => js or js => asp? Commented Sep 12, 2012 at 10:08

3 Answers 3

8

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
Sign up to request clarification or add additional context in comments.

2 Comments

This is equivalent to moment('2012-09-10 12:00PM').format('[/Date(]xZZ[))/]').
@Filipe Correia Thanks, very useful for me! But shouldn't it be '[/Date(]xZZ[)/]' (1 less parenthesis on the right)?
3

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') + ')';
}

Comments

1

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.com/a/12973157/1145963

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.