-1

I am trying to send datatime by Ajax to Controller.

var toDbViewModel = {
    "ClientId": clientId,
    "ProjectId": projectId,
    "TaskId": taskId,
    "Description": description,
    "Duration": totaldur,
    "Start": start.toISOString()
}

Every other vars are ok but "Start" equals "1/1/0001 12:00 AM"

What do I do wrong ?

--EDIT--

While sending from Website the date is : "2016-01-04"

miechooy
  • 3,178
  • 12
  • 34
  • 59
  • because "1/1/0001 12:00 AM" is a string, no? – Siddharth Feb 01 '16 at 08:14
  • yes it is but i did send another date – miechooy Feb 01 '16 at 08:16
  • 3
    Have you verified in the debugger that `start` has the expected value? – crashmstr Feb 01 '16 at 08:16
  • 1
    Also, `toISOString()` seems to be Javascript, so no c# here (i.e. is this tagged correctly)? – crashmstr Feb 01 '16 at 08:17
  • I'm not sure what exactly the question is, but I do want to verify that `DateTime.Parse("1/1/0001 12:00 AM")` works perfectly fine and DateTime is able to understand that format. – Owen James Feb 01 '16 at 08:18
  • Edited before C#, the date is correct – miechooy Feb 01 '16 at 08:20
  • What is the vale of `start` and what does `start.toISOString()` return (your your developer tools to debug your script). If the value when you bind the model is `1/1/0001 12:00 AM` then the data your sending is not a valid date. You should also show the controller method you posting to. –  Feb 01 '16 at 10:09

2 Answers2

1

I think, you get string "1/1/0001 12:00 AM" at server side because C# does not able to parse your passed date time value and take default DateTime value.

Below is the sample example to pass JSON data to server.

Server Side:

public class Person
{
    public string Name { get; set; }
    public DateTime Birthday { get; set; }
    public string[] Hobbies { get; set; }
}

Method:

public bool ProcessData(Person person)
{
    return person != null;
}

Client Side:

//POST the following JavaScript
var personObj = {
    name: "ABC",
    birthday: new Date(2016, 0, 1),
    hobbies: ["xyz", "pqr"]
};

$.ajax({
    url: "URL",
    type: "POST",
    contentType: "application/json",
    data: JSON.stringify({ person: personObj }),
    success: function(response) {
        response ? alert("It worked!") : alert("It didn't work.");
    }
});
111
  • 1,779
  • 1
  • 12
  • 15
0

You can send the date as a Time-Stamp instead, In Java >> http://www.tutorialspoint.com/java/util/date_gettime.htm

Ghayth
  • 894
  • 2
  • 7
  • 18
  • This might be helpful >> http://stackoverflow.com/questions/2683118/new-date-gettime-in-net – Ghayth Feb 01 '16 at 08:24