Javascript 如何格式化Json Date并将其绑定到DatePicker控件

Javascript 如何格式化Json Date并将其绑定到DatePicker控件,javascript,json,knockout.js,datepicker,momentjs,Javascript,Json,Knockout.js,Datepicker,Momentjs,我有json格式的数据数组: [{"AccountStatementID": 2,"CompanyID": 1,"Description": "test","Amount": 1000,"ReceiptDate": "/Date(1447261200000)/","Type": "Payment"}]; 我已成功地将数据加载到表中,并以正确的格式显示ReceiptDate(dd/MM/yyyy)。当我单击编辑按钮时,DatePicker控件中的ReceiptDate仍然是/Date(14472

我有json格式的数据数组:

[{"AccountStatementID": 2,"CompanyID": 1,"Description": "test","Amount": 1000,"ReceiptDate": "/Date(1447261200000)/","Type": "Payment"}];
我已成功地将数据加载到表中,并以正确的格式显示ReceiptDate
(dd/MM/yyyy)
。当我单击编辑按钮时,DatePicker控件中的ReceiptDate仍然是
/Date(1447261200000)/

我使用momentjs以以下方式格式化:

ReceiptDate: moment(self.ReceiptDate).format('DD/MM/YYYY')
此外,日期选择器不会出现

为什么ReceiptDate没有格式化,如何解决它并启动日期选择器


这是中的演示。

我看到您已将日期选择器设置为只读。这就是当点击时日期选择器没有出现的原因

我在你的小提琴上修改了一些细节,最后得到了这个

一个是在数据中保存Javascript日期时间对象,因为您正在调用矩并格式化它

var data = [
  {
    "AccountStatementID": 2,
    "CompanyID": 1,
    "Description": "test",
    "Amount": 1000,
    "ReceiptDate": new Date(1447261200000),
    "Type": "Payment"
  }
];

我想不出你在编辑时在哪里设置日期。因此,在编辑时调用矩().format()也将获得所需的格式。(如果需要,请在编辑时删除readonly to date)。

您的
foreach
循环使用原始数据填充的
AccountStatements

var data = [
    {
        "AccountStatementID": 2,
        "CompanyID": 1,
        "Description": "test",
        "Amount": 1000,
        "ReceiptDate": "/Date(1447261200000)/",
        "Type": "Payment"
    }
];

单击edit(编辑)时,将该原始数据的一条记录发送到edit(编辑)功能,并将其设置为
$root.AccountStatement

    self.AccountStatement(accountStatement);
所以你的编辑器只是在处理原始数据。在这个过程中的某个时刻,您需要将原始数据转换为您为单个私有变量创建的数据

var AccountStatement

我建议您创建一个函数,它接受原始数据记录并返回这种形式的内容。在分配
self.AccountStatements

之前,使用它转换输入数组。我设置为只读,因为我想从(日历)日期选择器获取日期值。如果单击“添加链接”,然后单击“收货日期”文本框,它将显示日历。从您提供的链接中,日历仍然没有显示。您没有删除readonly以更新现有数据。单击“添加新的对账单”,然后单击“输入收款日期”。
var AccountStatement