Donate

Custom JavaScript Date Sorter Function For Bootstrap Table in ASP.NET MVC

While working on a project that implements Bootstrap Table by Wenzhixin, one column returns a date value with the format of MMM-YYYY and this column is sortable. A solution I've come up is to use the flexibility of Moment.js. Here's to date sorter function for the column.
function DateSorter(a, b) {
    var dateA = new Date(moment(a).format('MMM-YYYY'));
    var dateB = new Date(moment(b).format('MMM-YYYY'));
    if (dateA < dateB)
        return -1;
    if (dateA > dateB)
        return 1;
    return -0;
}
Then set the data-sorter property of the table column with the function name.
<th data-field="BillingMonth" data-sortable="true" data-sorter="DateSorter">Billing Month</th>
Output
Custom JavaScript Date Sorter Function For Bootstrap Table in ASP.NET MVC
Sample Fiddle: Custom JavaScript Date Sorter Function For Bootstrap Table Using Moment.JS

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

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