Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatables.net date display format

I'm using datatables.net jquery plugin and populating the data through asp.net mvc4.

One of my columns is a date format. I'm interested in changing the format of how it is displayed but not changing the type of the column from date to string.

I'm currently using default initialization:

$('#data-table').dataTable();

My 4th column is a date "Created Date" and is currently displayed as "7/07/2013 9:38:11 p.m." I would like it to be displayed as either "7/07/2013" or "7 Jul 2013".

I thought this was a relatively easy task but haven't been able to find the answer I'm after.

I'd like to avoid additional plugins if possible. Is there a simple way that I'm missing?

like image 265
PostureOfLearning Avatar asked Jul 07 '13 09:07

PostureOfLearning


1 Answers

Here is what I came up with. Using how it renders I can manipulate the way the date is displayed to anyway I want.

$('#data-table').dataTable({
    "aoColumnDefs": [
        { 
            "aTargets": [0], 
            "bSearchable": false, 
            "sType": 'date',
            "fnRender": function ( oObj ) {
                var javascriptDate = new Date(oObj.aData[0]);
                javascriptDate = javascriptDate.getDate()+"/"+(javascriptDate.getMonth()+1)+"/"+javascriptDate.getFullYear();
                return "<div class='date'>"+javascriptDate+"<div>";
            }
        }
    ]
}); 

I hope this will be useful to others...

like image 80
PostureOfLearning Avatar answered Oct 07 '22 12:10

PostureOfLearning