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

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

问题描述

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

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

$('#userData').dataTable({

        "ajax": {
                "url": "${rc.getContextPath()}/module/roles/users-list",
                "dataSrc":  "",
                },

        "columns":[
        {"data": "userId"},
        {"data": "applicationId"},
        {"data": "username"},
        {"data": "firstName"},
        {"data": "userCreated"},
        {"data": "createdTime"},
        {"data": "updatedTime"}
        ],

     });

表中收到的数据是json,并且会像

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":"sitepmadm",
      "userModified":"sitepmadm",
      "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":"sitepmadm",
      "userModified":"sitepmadm",
      "createdTime":1422010854246,
      "updatedTime":1422010854246
   }

我希望将日期显示为正确的datetime。目前在json数据中被显示为同样的刺激。有什么办法可以在datatable中转换$ /

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

推荐答案

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

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

例如:

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

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

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