half_full half_full - 4 months ago 46
jQuery Question

DataTables how do I show only date rather than datetime

I'm using server side processing as per the Django example in DataTables. I return datetime values in this format 'yyyy-mm-dd hh:mm:ss'. These date time values are currently displayed like this (for example):


Dec. 18, 2011, 11:59 p.m.


I would like to display just the date part rather than both date and time.

This is what I have in my html page:

<script type="text/javascript">
/* <![CDATA[ */
$(document).ready(function() {
$('#certs-table').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "/cars/get_cars_list/",
"iDisplayLength": 10,
"aoColumnDefs": [
{ "aTargets": [0], "bVisible": false, "bSearchable": false},
{
"aTargets": [1],
"fnRender": function ( oObj ) {
return '<a href=\"/cars/' + oObj.aData[0] + '/\">' + oObj.aData[1] + '</a>';
},
"bSearchable": true,
},
{ "aTargets": [2], "bSearchable": true},
{ "aTargets": [3], "bSearchable": false, "sType": 'date'},
]
});
} );
/* ]]> */
</script>


The date is is the 4th column, i.e., aTarget[3].

How do I display just the date portion please? I only started using DataTables/JQuery yesterday, so would really appreciate an example. Thanks.

Answer

I think that the best whay would be to convert data server side and just return the date format you want, but you could also

 { "aTargets": [3], 
   "bSearchable": false, 
   "sType": 'date',
   "fnRender": function ( oObj ) {
              var javascriptDate = new Date(oObj.aData[0]);
              javascriptDate = javascriptDate.getDate()+"/"+javascriptDate.getMonth()+"/"+javascriptDate.getFullYear();
              return "<div class= date>"+javascriptDate+"<div>";
              }
 }
Comments