Passing datetime from javascript to C # (Controller)

How do you pass the date time (I need this to the second) in C # using jquery and mvc3. This is what I have

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

I can’t understand what format the date should be in order for C # to understand it.

+57
json jquery c # asp.net-mvc-3
Apr 2 2018-11-11T00:
source share
7 answers

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 } }); 
+27
Apr 02 2018-11-11T00:
source share

Try using toISOString (). It returns a string in ISO8601 format.

GET Method

Javascript

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

FROM#

 [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); }); 

FROM#

 [HttpPost] public JsonResult Do(DateTime date) { return Json(date.ToString()); } 
+76
Jan 29 '15 at 16:21
source share

try it

 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(); 
+1
Apr 02 2018-11-11T00:
source share

I found that I need to wrap a datetime string as follows:

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

It took me an hour to figure out how to enable the WCF service in order to return an error message stating that XD

0
Nov 16 '12 at 6:16
source share

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

 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
Jul 28 '19 at 19:38
source share

Convert json date to this format "mm / dd / yyyy HH: MM: ss"
dateFormat - jasondate format.js file found in blog.stevenlevithan.com

 var _meetStartTime = dateFormat(now, "mm/dd/yyyy HH:MM:ss"); 
-one
May 23 '11 at 16:20
source share
 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); } }); 
-one
May 05 '17 at 14:13
source share



All Articles