Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
637 views
in Technique[技术] by (71.8m points)

format json date to mm/dd/yy format before displaying in a jquery datatable

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...