0

In my MVC 4 project developed in VS2010, I have a screen that has a dropdown that shows date as dd-MMM-yyyy for the display text, with the underlying value as dd-mm-yyyy.

In the function that posts the data, I can see that the selected value is in dd-mm-yyy when I alert it out.

  alert($("#dropdwn_BirthDateVal").val());

This line above shows my date as desired in dd-mm-yyyy format.

However in the same method when I try to post that value to my controller

  $.ajax({
            type: "POST",
            url:"@Url.Content("~/Home/GetUserDetails")",
            async:false,
            dataType: "JSON",
            data: {
                 //.....other string and integer values
                 //.....that go thru properly
                "myDto.DOB":  $("#dropdwn_BirthDateVal").val()
            },
            error: function (error) {
                alert(error);

           },

           ..... remaining code here 

...the date comes in as 1/1/0001 12:00:00 AM

The controller action I am posting to, 'GetUserDetails' has a ViewModel class called UserVM as its paramter. UserVM has a dto class called MyDto. Within MyDto is a Date property called DOB of type DateTime. MyDto has other properties as well. The string and integer values go thru properly.

What am I missing?

user20358
  • 14,182
  • 36
  • 114
  • 186
  • Probably your server-side marshaller doesn't recognize or isn't expecting "dd-mm-yyyy" as a valid date pattern. I haven't worked in .net, is there someway to specify in your DTO the date pattern to use? – cfs May 13 '13 at 16:52

2 Answers2

5

You need to format the date as ISO-8601 when sending it to the server, e.g. 2011-12-19T15:28:46.493Z

This answer to this similiar question shows a number of way to accomplish this, my favorite is JSON.parse(JSON.stringify(new Date())) but that won't support IE7.

Community
  • 1
  • 1
Jason
  • 15,915
  • 3
  • 48
  • 72
  • if you convert it to string and send it to the controller, the parameter where it is being assigned is of type DateTime. Wont there be a problem? I am not at my development box now to try it out. – user20358 May 13 '13 at 18:32
  • No, the .net serializer will know how to handle oy based on the type being assigned to and the format oh the input string – Jason May 13 '13 at 21:02
1

The DateTime received turns into 1/1/0001 12:00:00 AM because the date format (dd-mm-yyyy) could not be parsed properly. When the framework cannot parse your date/time string the result is what you have above.

The result depends on the localization of your machine. For example, under en-US localization, the .NET framework expects the date format to be in MM/dd/yyyy by default. But you can explicitly specify the format of the date as follows.

using System.Globalization;

// dateString is the string in your dd-MM-yyyy format
public void GetValidDate(string dateString) 
{
    DateTime parsedDateTime;
    DateTime.TryParseExact(dateString, "dd-MM-yyyy", null, 
                               DateTimeStyles.None, out parsedDateTime)
}

Look at the MSDN documentation.

sakura-bloom
  • 4,524
  • 7
  • 46
  • 61
  • Thanks for the reply, Actually, I know its not getting parsed. Why is it not getting parsed and how to fix it was what I was looking for. I changed the location settings to US, UK, FR on the development pc and it all had the same effect. The date still came in as 1/1/0001 12:00:00 AM – user20358 May 14 '13 at 04:17