简体   繁体   中英

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" />

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)));

Following Maciej Grzyb's answer, Finally I got solution.

   var t = new Date(parseInt(DematRenouncedEntry.entrydate.replace("/Date(", "").replace(")/", ""), 10));

                                             var m = t.getMonth();
                                             var d = t.getDate();
                                             function addZ(m) { return m < 10 ? '0' + m : '' + m; };
                                             function addZy(d) { return d < 10 ? '0' + d : '' + d; };
                                             var y = t.getFullYear();
                                             var format = y + "." + addZ(m) + "." + addZy(d);
                                             self.entrydate(format);

I'd check out this: https://stackoverflow.com/a/18555136/1455010

Change the Json converter to format the date in ISO format: 2016-06-16T18:52:36+00:00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM