Search code examples
javascriptc#asp.netknockout.jsbootstrap-datetimepicker

Date Binding in Asp.Net using Knockout


I am having problem in binding datetime in beatpicker while fetching data from the database. In the picker it renders as: "/Date(1465323300000)/" , the KOJS as:

DematRenounced.js

 if (obj.ResponseData != null) {
                                                 if (obj.ResponseData.length > 0) {
                                                     var DematRenouncedEntry = obj.ResponseData[0];
                                                     
                                                     self.entrydate(DematRenouncedEntry.entrydate);
                                               
                                             }

and View as:

DematRenouncedEntry.aspx

  <input type="text" id="txtEntryDate" data-beatpicker="true" class="form-control"
                                        data-bind="value:entrydate" maxlength="10" onblur="return valFutureDate(this,'Y',true);"
                                        onpaste="return false" onkeypress="return isNumberKey(event)"
                                        placeholder="YYYY.MM.DD" />

Solution

  • The data returned from the server is apparently serialized using Microsoft JsonSerializer that uses a non-standard format when serializing DateTime properties. See this answer for more details: https://stackoverflow.com/a/726869/4602079.

    What you need to do before you can do anything with the date on the client is to parse it as Date. In your case you could modify DematRenounced.js as follows:

    self.entrydate(new Date(parseInt(DematRenouncedEntry.entrydate.replace("/Date(", "").replace(")/",""), 10)));