I have a basic SpringBoot app., embedded Tomcat, Thymeleaf template engine I want to order 1 date column of a datatable.
in my POJO:
public String getTimeFormatted() {
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("EEEE, MMMM d,yyyy h:mm,a", Locale.ENGLISH);
LocalDateTime dateTime = LocalDateTime.ofEpochSecond(time, 0, ZoneOffset.UTC);
return dateTime.format(formatter);
}
in the template:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script src="https://cdn.datatables.net/plug-ins/1.10.15/sorting/datetime-moment.js"></script>
<script th:inline="javascript">
$(document).ready(function() {
$.fn.dataTable.moment( 'EEEE, MMMM d,yyyy h:mm,a' );
$('#table').dataTable( {
"bLengthChange": false,
"pageLength": 25,
});
} );
</script>
but it does not order properly
This is quite easy to debug.
I even made a simple example.
You are using a format as EEEE, MMMM d,yyyy h:mm,a
in your code (I assume in spring) but you forgot to translate that into moment
format... and from the docs, that should be: dddd, MMMM D,YYYY h:mm,a
so the code should actually be:
$.fn.dataTable.moment("dddd, MMMM D,YYYY h:mm,a");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With