Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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":"[email protected]",
      "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":"[email protected]",
      "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

like image 216
Geo Thomas Avatar asked Feb 26 '15 02:02

Geo Thomas


People also ask

How to convert JSON date into valid date format using JavaScript/jQuery?

now I want to convert it into valid date format like dd/MM/yyyy (25/09/2017), so how can I convert JSON date into valid date format using javascript or jquery? With the help of javascript create a function like below and pass the above JSON date ( /Date (1224043200000)/ ) to convert it into mm/DD/yyyy

How to format date and time in Excel using dynamic content?

The date is formatted to MDY mmddyyyy. Go to the expression editor Add dynamic content – click on Expression tab Type formatDateTime or look under Date and time functions Provide the value can be dynamic content to be formatted surrounded by single quotes Provide the format string surrounded by single quotes The full expression should look like.

What is the best date format for a JavaScript file?

The ISO 8601 syntax YYYY-MM-DD is also the preferred JavaScript date format. Converters provide custom support for serializing and deserializing with JsonSerializerCustom support can also be implemented when using Utf8JsonReader and Utf8JsonWriter.

How to show the time along with the date in jQuery?

For showing the time along with the date add the code below: I always use moment.js ( http://momentjs.com/) when dealing with dates in js. The date values returned are in unix timestamp so you need to convert them. @Suraj Gulhane --your answer is the only one I could get to work for ASP.NET Core Server-Side jQuery DataTables DateTime formatting.


1 Answers

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();
    }
}
like image 74
nhkhanh Avatar answered Oct 04 '22 21:10

nhkhanh