在 jquery 数据表中显示之前将 json 日期格式化为 mm/dd/yy 格式 [英] format json date to mm/dd/yy format before displaying in a jquery datatable

查看:18
本文介绍了在 jquery 数据表中显示之前将 json 日期格式化为 mm/dd/yy 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在数据表中显示一些数据,我使用的表脚本是

$('#userData').dataTable({阿贾克斯":{网址":我的网址",数据源":",},列":[{数据":用户 ID"},{数据":applicationId"},{数据":用户名"},{数据":名字"},{数据":用户创建"},{数据":创建时间"},{数据":更新时间"}],});

表接收到的数据是json,类似于

<预><代码>[{用户 ID":179,applicationId":pgm-apn",用户名":collaborator.user3",密码":密码1",电子邮件":user@xample.com",名字":安东尼",姓氏":贡萨尔维斯",启用":真,用户创建":gtuser",用户修改":gtuser",创建时间":1422454697373,更新时间":1422454697373},{用户 ID":173,applicationId":pgm-apn",用户名":consumer.user",密码":密码1",电子邮件":test@egc.com",名字":夏洛克",姓氏":家",启用":真,用户创建":gtuser",用户修改":gtuser",创建时间":1422010854246,更新时间":1422010854246}

我想将日期显示为正确的日期时间.目前它在json数据中显示为相同的刺痛.有没有办法在数据表中转换它

解决方案

您可以使用render"属性来格式化您的列显示 http://datatables.net/reference/option/columns.render#function.

例如:

<代码>{数据":创建时间",渲染":函数(数据){var date = new Date(data);var 月份 = date.getMonth() + 1;return (month.toString().length > 1 ?month : "0" + month) + "/" + date.getDate() + "/" + date.getFullYear();}}

I am trying to display some data in a datatable and the table script I am using is

$('#userData').dataTable({
     
        "ajax": {
                "url": "my-url",
                "dataSrc":  "",
                },
                
        "columns":[
        {"data": "userId"},
        {"data": "applicationId"},
        {"data": "username"},
        {"data": "firstName"},
        {"data": "userCreated"},
        {"data": "createdTime"},
        {"data": "updatedTime"}
        ],
        
     });

the data that is received by the table is json and would be something like

[
 {  
      "userId":179,
      "applicationId":"pgm-apn",
      "username":"collaborator.user3",
      "password":"password1",
      "email":"user@xample.com",
      "firstName":"Anthony",
      "lastName":"Gonsalves",
      "enabled":true,
      "userCreated":"gtuser",
      "userModified":"gtuser",
      "createdTime":1422454697373,
      "updatedTime":1422454697373
   },
   {  
      "userId":173,
      "applicationId":"pgm-apn",
      "username":"consumer.user",
      "password":"password1",
      "email":"test@egc.com",
      "firstName":"sherlock ",
      "lastName":"homes",
      "enabled":true,
      "userCreated":"gtuser",
      "userModified":"gtuser",
      "createdTime":1422010854246,
      "updatedTime":1422010854246
   }

I want to display the dates as proper datetime.Currently it is getting displayed as teh same sting in the json data.Is there any way to convert that in the datatable

解决方案

You can use "render" property to format your column display http://datatables.net/reference/option/columns.render#function.

For example:

{
    "data": "createdTime",
    "render": function (data) {
        var date = new Date(data);
        var month = date.getMonth() + 1;
        return (month.toString().length > 1 ? month : "0" + month) + "/" + date.getDate() + "/" + date.getFullYear();
    }
}

这篇关于在 jquery 数据表中显示之前将 json 日期格式化为 mm/dd/yy 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆