Pass a datetime from javascript to c# (Controller)

前端 未结 7 2206
小蘑菇
小蘑菇 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:12

    The following format should work:

    $.ajax({
        type: "POST",
        url: "@Url.Action("refresh", "group")",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ 
            myDate: '2011-04-02 17:15:45'
        }),
        success: function (result) {
            //do something
        },
        error: function (req, status, error) {
            //error                        
        }
    });
    
    0 讨论(0)
  • 2020-12-08 02:20
    var Ihours = Math.floor(TotMin / 60);          
    

    var Iminutes = TotMin % 60; var TotalTime = Ihours+":"+Iminutes+':00';

       $.ajax({
                url: ../..,
                cache: false,
                type: "POST",                
                data: JSON.stringify({objRoot: TotalTime}) ,
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                success: function (response) {
    
                },
                error: function (er) {
                    console.log(er);
                }
            });
    
    0 讨论(0)
  • 2020-12-08 02:21

    I found that I needed to wrap my datetime string like this:

    "startdate": "\/Date(" + date() + ")\/"
    

    Took me an hour to figure out how to enable the WCF service to give me back the error message which told me that XD

    0 讨论(0)
  • 2020-12-08 02:24

    Update: Please see marked answer as a better solution to implement this. The following solution is no longer required.

    Converting the json date to this format "mm/dd/yyyy HH:MM:ss"
    dateFormat is a jasondate format.js file found at blog.stevenlevithan.com

    var _meetStartTime = dateFormat(now, "mm/dd/yyyy HH:MM:ss");
    
    0 讨论(0)
  • 2020-12-08 02:25

    try this

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

    In C#

    DateTime.Now.ToUniversalTime().AddMinutes(double.Parse(MyDate)).ToString();
    
    0 讨论(0)
  • 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                        
               }
           });
    
    0 讨论(0)
提交回复
热议问题