How to properly sort standard time hh:mm:ss AM/PM
How to properly sort standard time hh:mm:ss AM/PM
Hello, I am having issues figuring out how I can sort one of my columns that contain time like hh:mm:ss am or it will show pm. It appears the time is sorting 1pm as being lower than 9am ect.... I did see that using moment.js can help and I tried the following,
Current code:
$.fn.dataTable.moment( 'h:mm:ss A');
$('#myTable').DataTable( {
dom: 'Bfrtip', //Needed to display buttons
buttons: [
'copy', 'csv', 'pdf'
]
});
$('.btn-secondary').css('background', '#FFF');
$('.btn-secondary').css('color', '#111');
Still not working correctly.
Not sure if it helps but I am using Node.js with express and EJS. I am storing the data locally to MongoDB and fetching the data with an api route and using ejs to display the date in the table. The date sorts correctly just not the time. Example start_time is saved in the db as "9:48:41 am" and displays on the UI the same way.
<td><%= results.results[0].start_date %></td>
<td><%= results.results[0].start_time %></td>
Answers
Hi,
I had a similar issue. Take a look at the "Computed Value" section of Orthogonal Data
One part of the solution is to use the options for
columns
The other part of the solution is on line six where:
var d = new Date( data * 1000 );
Instead of multiplying by 1000 I would do:
var d = new Date( data ).getTime();
The getTime() function simply returns a numerical value based on Date and Time.
A more complete example based on your own code would be:
The plug-in introduced in this blog post is how I'd typically recommend you do date time sorting (unless you have the integer data available for orthogonal sorting data).
Allan
Hi @markApps ,
It would be worth looking at this plugin here, this is designed for ordering on time. This uses Moment.js, a good JS date/time library.
Cheers,
Colin
I think I am almost there but for the Start Date column it states the the date is invalid. I am seeing this in the console.
The warning message says this:
If you can give us a link to a page showing the issue that would be useful.