Pass a datetime from javascript to c# (Controller)

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

    Try to use toISOString(). It returns string in ISO8601 format.

    GET method

    javascript

    $.get('/example/doGet?date=' + new Date().toISOString(), function (result) {
        console.log(result);
    });
    

    c#

    [HttpGet]
    public JsonResult DoGet(DateTime date)
    {
        return Json(date.ToString(), JsonRequestBehavior.AllowGet);
    }
    

    POST method

    javascript

    $.post('/example/do', { date: date.toISOString() }, function (result) {
        console.log(result);
    });
    

    c#

    [HttpPost]
    public JsonResult Do(DateTime date)
    {
         return Json(date.ToString());
    }
    
    0 讨论(0)
提交回复
热议问题