38

MY Ajax call

 $('#QuickReserve').click(function () {
        var now = new Date();
        alert(now);

        var _data = {
            'ComputerName': _computerName,
            '_mStart': now.toTimeString(),
            '_mEnd': now.toDateString()
        };
        $.ajax({
            cache: false,
//            contentType: "application/json; charset=utf-8",
            type: "POST",
            async: false,
            url: "/Home/SetMeeting",
            dataType: "json",
            data: _data,
            success: "",
            error: function (xhr) {
                alert("Error");
                alert(xhr.responseText);
            }
        });
    });

MY C# code

 public ActionResult SetMeeting(string ComputerName, DateTime? _mStart, DateTime? _mEnd)
        {
           }

DateTime values are not received at code end..... They just appear blank. In jquery when i tried to

'_mStart': now.toTimeString(),
            '_mEnd': now.toDateString()

to datestring does return today's date, but, i want time part of date time also.

Nanu
  • 3,010
  • 10
  • 38
  • 52
  • 1
    You should try to accept strings instead of datetime and then just parse the strings to dates after that. The issue is probably just a parsing issue – Oskar Kjellin May 20 '11 at 19:59

5 Answers5

88

Don't do any tricks with data formats. Just use standard function date.toISOString() which returns in ISO8601 format.

from javascript

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

from c#

[HttpPost]
public JsonResult Do(DateTime date)
{
     return Json(date.ToString());
}
rnofenko
  • 9,198
  • 2
  • 46
  • 56
  • 1
    This is the correct answer, the marked answer is depend on the server's system date format. – Luke Vo Jul 19 '15 at 09:08
  • Can't use this you want to receive the date from a datepicker. Any alternatives for when I want to receive a date from a datepicker? – Janco de Vries Jun 11 '19 at 07:58
12

Converting the json date to this format "mm/dd/yyyy HH:MM:ss" is the whole trick dateFormat is a function in jsondate format.js file found at http://blog.stevenlevithan.com/archives/date-time-format

var _meetStartTime = dateFormat(now, "mm/dd/yyyy HH:MM:ss");
Nanu
  • 3,010
  • 10
  • 38
  • 52
1

Can you not just pass one DateTime up, and separate the Date part from the time part on the server?

Can you not just pass '_mDate': now;

public ActionResult SetMeeting(string ComputerName, DateTime? _mDate)
{
   // Then in here use _mDate.Date, and _mDate.Time    
}
ETFairfax
  • 3,794
  • 8
  • 40
  • 58
1

Why not convert the date (and time) into a timestamp from Unix Epoch and then use js to display the date?

c#

public double FromUnixEpoch(DateTime value)
{
    DateTime unixEpoch = new DateTime(1970, 1, 1);
    double timeStamp = (value - unixEpoch).Ticks / 1000;
    return timeStamp;
}

js

var myDate = new Date( object.myEpochDate *1000);
myDate.toUTCString().toLocaleString();

With this approach you can pass the epoch as a string inside json and then handle it like a date in js.

gnome
  • 1,113
  • 2
  • 11
  • 19
  • 1
    I want to pass date from js to c#, and not the other way around. – Nanu May 23 '11 at 14:54
  • Sorry Nikhil, misunderstood you. Is JS convert the datetime to unix epoch, var myDate= new Date(); var unixEpoch = myDate.getTime(); In C# convert it from unix epoch: public DateTime fromUnixEpoch(long epoch) { return (new DateTime(1970, 1, 1, 0, 0, 0)).AddSeconds(epoch); } – gnome May 24 '11 at 18:29
  • I came up with an easy solution, i formatted the json date to the "mm/dd/yyyy HH:MM:ss" format, and it worled like magic. Thanks – Nanu May 24 '11 at 21:32
0

It has to do with formatting. You can definitely use libs or plugins, I chose to keep it really simple:

function getFormattedDate(date) {
  var curr_date = date.getDate();
  var curr_month = date.getMonth() + 1; //Months are zero based
  var curr_year = date.getFullYear();
  return curr_date + "-" + curr_month + "-" + curr_year;          
}

KISS!

Jowen
  • 5,203
  • 1
  • 43
  • 41