Posts

Showing posts with the label Moment.js

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-searchabl

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

Image
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 Sample Fiddle: Custom JavaScript Date Sorter Function For Bootstrap Table Using Moment.JS

Sort Bootstrap Table Date Column By Using Moment.js In ASP.NET MVC

Image
Hello, In this tutorial, I'll demonstrate on how to sort a date column of a Bootstrap Table by Wenzhixin using Moment.js. First is to create an empty ASP.NET MVC project and then add an ADO.NET Entity Model that references the Employee table of Northwinds database. Next is to define an Employee ViewModel class that will hold the information to be utilized by the Bootstrap table. public class EmployeeViewModel { public int EmployeeID { get ; set ; } public string EmployeeName { get ; set ; } public DateTime BirthDate { get ; set ; } public DateTime HireDate { get ; set ; } public string Address { get ; set ; } public string PostalCode { get ; set ; } public string Country { get ; set ; } } Then add a new controller called Employee that declares a Northwind context object, queries the database and returns the model object as JSON. public class EmployeeController : Controller { private NorthwindEntities _context; public EmployeeController() { _con

Donate