momentjs working with unix timestamps
momentjs working with unix timestamps
Hi, I'm populating DataTables with data which includes dates in unix format (eg 1496310558000 = Thursday 1st June 2017 09:49:18 AM UTC). I'm able to display this with the following code inside date columns:
render: function(data, type, row){
if(data){
return moment.unix(parseInt(data)/1000).format('L');
}
}
However this breaks sorting, since clicking the column header sorts as text, not as a date. I'm trying to use the datetime-moment.js plugin to handle this for me. I can't get it working though, presumably because the initialiser only wants the format and locale, and doesn't know how to handle unix. The dates just appear in the table as the timestamp. Here's an example:
var $j = jQuery.noConflict();
$j.fn.dataTable.moment( 'L, LTS', 'en-gb' );
var myCasesTbl = $j('#myCases').DataTable({
columns: [
{data: 'Id', visible: false, searchable: false},
{data: 'CaseNumber', visible: true, searchable: true, defaultContent: ''},
{data: 'Subject', visible: true, searchable: true, defaultContent: ''},
{data: 'Status', visible: true, searchable: true, defaultContent: ''},
{data: 'Comment_Last_Modified_Date', visible: true, searchable: true, defaultContent: ''}
],
rowId: 'Id',
order: [[4, 'asc']],
});
In that table, Comment_Last_Modified_Date is the timestamp. I get no errors but the dates just appear as '1493897859000'
This question has an accepted answers - jump to answer
Answers
Since you are using
columns.render
as a function already, you just need to adjust it slightly to use orthogonal data.Have the function return the formatted string for
display
andfilter
data types, and the integer value for all others.Allan
Thanks I'll look into that and feed back to you. In your examples you always include moment.min.js as well as datetime-moment.js. Is it possible for this to work with moment-with-locales.min.js and moment-timezone-with-data.min.js?
Oh man you're the best, that's so EASY! Thanks!