在 Kendo UI Grid 中将 UNIX 时间戳格式化为人类日期的正确方法是什么?

Lee*_*ler 5 timestamp date unix-timestamp kendo-ui kendo-grid

好吧,似乎有很多类似的问题,但我找不到可以回答这个特定问题的问题..所以这里是..

有一个可用的 Kendo UI 网格。我的数据源正在返回一个时间戳 - 这是返回到代码的 JSON 响应:

在此处输入图片说明

您会注意到下一行也是一个日期.. 由 MySQL 作为标准 DateTime 格式返回 - 我很乐意直接使用它。但我已将日期转换为我认为更通用的时间戳。(??)

现在我需要做两件事 - 将时间戳格式化为可读日期并编辑日期,以便将其保存回数据源。但让我们先解决格式问题。

我当前显示列的代码如下所示:

    {   title: "Trial<br>Date", 
    field: "customer_master_converted_to_customer_date",
    format: "{0:d/M/yyyy}",
    attributes: {
        style: "text-align: center; font-size: 14px;"
    },
    filterable: true,
    headerAttributes: {
        style: "font-weight: bold; font-size: 14px;"
    }
},
Run Code Online (Sandbox Code Playgroud)

虽然我试过了..

    toString(customer_master_converted_to_customer_date, "MM/dd/yyyy")
Run Code Online (Sandbox Code Playgroud)

.. 以及它的几种变体 - 在格式字符串方面。是的,我试过输入:

    type: "date",
Run Code Online (Sandbox Code Playgroud)

无论我做什么,我都只得到时间戳。

在此处输入图片说明

任何人?

Ata*_*hev 4

您需要首先将时间戳转换为 JavaScript 日期。这是一个示例实现:

$("#grid").kendoGrid({
  dataSource: {
    data: [
      { date: 1371848019 }
    ],
    schema: {
      model: {
        fields: {
          date: {
            type: "date",
            parse: function(value) {
              return new Date(value * 1000);
            }
          }
        }
      }
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

这是现场直播: http: //jsbin.com/utonil/1/edit