简体   繁体   English

使用Knockout在Asp.Net中进行日期绑定

[英]Date Binding in Asp.Net using Knockout

I am having problem in binding datetime in beatpicker while fetching data from the database. 从数据库中获取数据时,在beatpicker中绑定日期时间时遇到问题。 In the picker it renders as: "/Date(1465323300000)/" , the KOJS as: 在选择器中,它呈现为:“ / Date(1465323300000)/”,KOJS呈现​​为:

DematRenounced.js 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 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. 从服务器返回的数据显然是使用Microsoft JsonSerializer序列化的,该序列化DateTime属性时使用非标准格式。 See this answer for more details: https://stackoverflow.com/a/726869/4602079 . 有关更多详细信息,请参见此答案: 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. 在客户端上执行任何操作之前,您需要做的就是将其解析为Date。 In your case you could modify DematRenounced.js as follows: 您可以按照以下方式修改DematRenounced.js:

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

Following Maciej Grzyb's answer, Finally I got solution. 遵循Maciej Grzyb的回答,终于得到了解决方案。

   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 我会检查一下这个: https : //stackoverflow.com/a/18555136/1455010

Change the Json converter to format the date in ISO format: 2016-06-16T18:52:36+00:00 更改Json转换器以将日期格式化为ISO格式:2016-06-16T18:52:36 + 00:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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