-1

I am working on an asp.net mvc-4 web application. and i have the following fields:-

<div>    
@Html.TextBoxFor(model => model.LiveDate, new { @class = "datepicker", @Value = (Model != null && Model.LiveDate.HasValue) ? Model.LiveDate.Value.ToString("dd/MM/yyyy") : string.Empty })
@Html.ValidationMessageFor(model => model.LiveDate)
</div>

<div>
@Html.TextBoxFor(model => model.EndDate, new { @class = "datepicker", disabled = "disabled",@Value = (Model != null && Model.EndDate.HasValue) ? Model.EndDate.Value.ToString("dd/MM/yyyy") : string.Empty }) 
@Html.ValidationMessageFor(model => model.EndDate)
</div>

<div>     
@Html.EditorFor(model => model.ContractLength) 
@Html.ValidationMessageFor(model => model.ContractLength)
</div>

now i defined the following jquery which should send the LiveDate & ContractLength to an action method to set the EndDate:-

$("#LiveDate, #ContractLength").change(function () {
            $.getJSON("@Url.Content("~/Order/CalculateDate")", { date: $("#LiveDate").val(), month: $("#ContractLength").val() },

            function (Data) {

                $("#EndDate").val(Data.Name);


            });
        });

and here is the realted action method which will do the calculationspublic

 public   ActionResult CalculateDate(string date, int? month)
             {
                 if (!String.IsNullOrEmpty(date) && month.HasValue)
                 {
                     DateTime d = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                     var Data = new { Name = d.AddMonths(month.Value) };
                     return Json(Data, JsonRequestBehavior.AllowGet);
                 }
                 return Json(new { Name=string.Empty}, JsonRequestBehavior.AllowGet);




             }

now i tried this:-

  1. the livedate = 31/05/2016
  2. contract length in month= 7
  3. the action method return the following 31/12/2016 00:00:00 but inside the EndDate i got this value /Date(1483142400000)/ instead if "31/12/2016".. so can anyone advice accordingly ?
John John
  • 1
  • 72
  • 238
  • 501
  • 1
    This is the default serialisation behavior for MVC. [See this answer on StackOverflow for options.](http://stackoverflow.com/questions/726334/asp-net-mvc-jsonresult-date-format) – John Mc May 17 '16 at 15:18
  • @JohnMc and how i can parse the serialization ? – John John May 17 '16 at 15:22
  • you should be accepting a DateTime object instead of a string. It will work once you've set the Json serialisation settings on something like Newton Soft Json.Net. http://www.newtonsoft.com/json/help/html/datesinjson.htm – John Mc May 17 '16 at 15:24
  • @JohnMc but inside my view i am parsing the date time to a string using .ToString() ... that why if i modify the action method to accept datetime i will always receive null.. – John John May 17 '16 at 15:26

1 Answers1

0

you are getting a timestamp, use javascript functions

  • toDateString
  • toTimeString
  • toLocaleString

or new Date(timeStamp) for getting a date object.

I think just var csDate = new Date('1483142400000'); will do the job.

Note that if dd/MM/yyyy is important as a format your options for formating that date object will be :

  • a custom function implementation that does the job of converting
  • or a library like http://momentjs.com/
Ermir Beqiraj
  • 867
  • 11
  • 24