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

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.

Invalid nested tag div found, expected closing tag input