【问题标题】:What is the proper way to format a UNIX timestamp into a human date in Kendo UI Grid?在 Kendo UI Grid 中将 UNIX 时间戳格式化为人类日期的正确方法是什么?
【发布时间】:2013-08-04 08:53:58
【问题描述】:

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

有一个有效的 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;"
    }
},

虽然我试过了..

    toString(customer_master_converted_to_customer_date, "MM/dd/yyyy")

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

    type: "date",

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

有人吗?

【问题讨论】:

标签: date kendo-ui timestamp kendo-grid unix-timestamp


【解决方案1】:

您需要先convert 将时间戳记到 JavaScript 日期。这是一个示例实现:

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

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

【讨论】:

  • 感谢阿塔纳斯的回复。但是,这不应该完成同样的事情..假设您不将日期转换为 unix 时间戳? title: "Trial&lt;br&gt;Date", field: "customer_master_converted_to_customer_date", type: "date", template: " #= kendo.toString(customer_master_converted_to_customer_date, 'M/d/yyyy') # ", attributes: { style: "text-align: center; font-size: 14px;" }, filterable: true, headerAttributes: { style: "font-weight: bold; font-size: 14px;" }
  • 不,您不能将数字格式化为日期。你需要先解析它。
  • 如果返回的项目不是 unix 时间戳,而是日期......就像我在时间戳下方的示例中显示的那样?
  • 我不明白这个问题。请澄清。
  • 对不起.. 应该更具体。因此,如果您查看原始问题-我有一个指向时间戳的图形。正下方是返回的常规日期值。我在问题中提到了它。所以我的评论是询问对于常规日期项目是否同样适用(在剑道中它被指定为日期)。您不应该能够格式化日期项(指定为日期项)吗?这似乎也不起作用。
【解决方案2】:

我刚刚遇到了同样的问题,我试过了,现在完美无缺,祝你好运。

template :#= kendo.toString(new Date(parseInt(dateOfBirth)), 'yyyy-MM-dd')#"

dateOfBirth 是要格式化的日期,结果会是这样的:2015-09-11。 祝你好运。

【讨论】:

    【解决方案3】:

    谢谢@Atanas Korchev,这对我有用,这就是我最终要做的:

    // in datasource
        schema:{
          model: {
            fields: {
              date: { type: "date",
                        parse: function(value) {
                          return new Date(value);
                        }
              }, // … other fields
    
        // in columns
        columns: [
            {
              field: "date",
              title: "Date",
              type: "date",
              format: "{0:dd MMM yyyy}"
            },
            // ... other columns
         ]

    【讨论】:

      【解决方案4】:

      将数据库中的时间戳格式数据用于 KendoGrid 的最简单方法。

      https://stackoverflow.com/a/67106362/5671943

      <kendo-grid-column class="CreatedAt" field="CreatedAt" title="title"
       [width]="120" [headerStyle]="{'background-color': '#36455a','color': '#fff'}"
       [style]="{'background-color': '#36455a','color': '#fff'}">
            <ng-template kendoGridCellTemplate let-dataItem>
               {{dataItem.CreatedAt | date:"yyyy/MM/dd HH:mm:ss"}}
            </ng-template>
      </kendo-grid-column>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-28
        • 1970-01-01
        • 1970-01-01
        • 2020-10-23
        • 2014-06-09
        • 1970-01-01
        • 2012-04-19
        相关资源
        最近更新 更多