Donate

Custom JavaScript Date Time Sorter and Date Time Formatter Function For Bootstrap Table in ASP.NET MVC Using Moment.JS

 Hello Fellow Developers,

Here's how to format and sort dates with time values using Bootstrap Table by Wenzhixin in ASP.NET MVC. Assuming that the values returned from the database have hours, minutes and seconds.

DateTimeFormatter
function DateTimeFormatter(value, row, index) {
    if (value === null || value === undefined || value === "") {
        return "";
    }
    else {
        return moment(value).format('YYYY-MM-DD HH:mm:ss');
    }
}
DateTimeSorter
function DateTimeSorter(a, b) {
    var dateA = new Date(moment(a).format('YYYY-MM-DD HH:mm:ss'));
    var dateB = new Date(moment(b).format('YYYY-MM-DD HH:mm:ss'));
    if (dateA < dateB)
        return -1;
    if (dateA > dateB)
        return 1;
    return -0;
}
Usage
<th data-field="LastLogin" data-sortable="true" data-sorter="DateTimeSorter" data-formatter="DateTimeFormatter" data-searchable="true">Last Login</th>

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

Bootstrap Modal In ASP.NET MVC With CRUD Operations