You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:无法将Kendo Grid的DateTime格式改为dd/MM/yyyy

解决Kendo Grid日期字段格式不生效问题

你的问题核心在于:Kendo Grid的format配置仅对原生Date类型的数据生效。如果数据源中的TGL_MASUK是ISO格式的字符串(尽管看起来像DateTime),Kendo不会自动识别为Date类型,导致格式配置失效。

以下是两种可行的解决方案:

方案一:在数据源Schema中指定字段类型

修改你的数据源配置,明确将TGL_MASUK字段定义为Date类型,并添加解析函数处理ISO字符串:

// 调整数据源配置
settingModel.ds_grid_dataSource = new kendo.data.DataSource({
  // 原有数据源配置(如transport、pageSize等)
  schema: {
    model: {
      fields: {
        NRP: { type: "string" },
        NAMA: { type: "string" },
        SUBCONT_CODE: { type: "string" },
        DISTRICT: { type: "string" },
        // 重点:指定TGL_MASUK为date类型并解析
        TGL_MASUK: { 
          type: "date",
          parse: function(value) {
            return new Date(value);
          }
        }
      }
    }
  }
});

完成后,你原来的列format: "{0:dd/MM/yyyy}"配置就会正常生效。

方案二:使用列Template手动格式化

如果无法修改数据源配置,直接在列配置中通过template手动转换并格式化:

$("#grid").kendoGrid({
        dataSource: settingModel.ds_grid_dataSource,
        resizable: "true",
        editable: "inline",
        scrollable: "true",
        sortable: "true",
        filterable: "true",
        pageable: "true",
        height: "500px",
        columns: [
            { field: 'NRP', title: 'NRP', width: 30 },
            { field: 'NAMA', title: 'Name', width: 70 },
            { field: 'SUBCONT_CODE', title: 'Subcont', width: 50 },
            { field: 'DISTRICT', title: 'District', width: 50 },
            // 替换为template配置
            { 
              field: 'TGL_MASUK', 
              title: 'Tanggal Masuk', 
              template: "#= kendo.format('{0:dd/MM/yyyy}', new Date(TGL_MASUK)) #", 
              width: 50 
            }
        ]
    }).data("kendoGrid");

验证步骤

可以在浏览器控制台执行以下代码,确认TGL_MASUK的实际类型:

console.log(typeof settingModel.ds_grid_dataSource.data()[0].TGL_MASUK);
  • 如果输出string:说明是字符串类型,需要用上述方案处理
  • 如果输出object:说明是Date对象,检查是否有其他配置覆盖了格式(如全局文化设置)

内容的提问来源于stack exchange,提问作者Hanung Risqi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 12:20:41