Pass a datetime from javascript to c# (Controller)

前端 未结 7 2209
小蘑菇
小蘑菇 2020-12-08 02:01

How do you pass a date time (i need it to the second) to c# using jquery and mvc3. This is what I have

var date = new Date();    
$.ajax(
   {
       type: \         


        
7条回答
  •  一向
    一向 (楼主)
    2020-12-08 02:28

    There is a toJSON() method in javascript returns a string representation of the Date object. toJSON() is IE8+ and toISOString() is IE9+. Both results in YYYY-MM-DDTHH:mm:ss.sssZ format.

    var date = new Date();    
        $.ajax(
           {
               type: "POST",
               url: "/Group/Refresh",
               contentType: "application/json; charset=utf-8",
               data: "{ 'MyDate': " + date.toJSON() + " }",
               success: function (result) {
                   //do something
               },
               error: function (req, status, error) {
                   //error                        
               }
           });
    

提交回复
热议问题