EasyUI Forum
May 02, 2024, 11:35:15 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Datagrid Datetime Column Sort [Solved]  (Read 3865 times)
Darrel
Jr. Member
**
Posts: 74


View Profile
« on: October 30, 2018, 01:56:03 PM »

Hello Team,

Basically I wanted to try out sorting on the datagrid column on a date field. The following url that I referred for doing this custom sorting is https://www.jeasyui.com/tutorial/datagrid/datagrid14.php

I had to tweak it a little for dd/mm/yyyy format. But when I introduced the timestamp into the date field, that's when the results started getting weird. The same can be checked in the following fiddle: http://jsfiddle.net/c9so6p3b/

You can change the sorting for the date field and check the results. The output isn't as expected, notice the timestamp part. Please could you help me out. One thing I have in mind is to separate out the time stamp before doing the comparison but then I got stuck on when I need to add it back Sad

Also just another thought, how would this work in case the date string is in dd/Mon/yyyy format....

Regards,
Darrel
« Last Edit: October 31, 2018, 10:23:32 PM by Darrel » Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 on: October 30, 2018, 05:58:42 PM »

Try to define the 'sorter' function as:
Code:
sorter:function(date1,date2){
  var toDate = function(str){
    var parts = str.split(' ');
    var dd = parts[0].split('/');
    var hh = parts[1].split(':');
    var date = new Date(dd[2],parseInt(dd[1])-1,dd[0],hh[0],hh[1],hh[2]);
    return date;
  }
  var d1 = toDate(date1);
  var d2 = toDate(date2);
  return d1.getTime()<d2.getTime()?-1:1;
}
Logged
Darrel
Jr. Member
**
Posts: 74


View Profile
« Reply #2 on: October 31, 2018, 10:23:17 PM »

Hello stworthy,

Thanks a lot for your reply. It works as expected.

The below code is my modified version which handles the date in dd/Mon/yyyy format as well.

Code:
var month_names = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];

function dateSorter(date1, date2)
{
var toDate = function(str){
var parts = str.split(' ');
var dd = parts[0].split('/');
var hh = parts[1].split(':');
var date = "";
try{
date = new Date(dd[2],month_names.indexOf(dd[1].substring(0,3).toLowerCase()),dd[0],hh[0],hh[1],hh[2]); //converting month to lower case
}
catch(Ex){
date = new Date(dd[2],parseInt(dd[1])-1,dd[0],hh[0],hh[1],hh[2]); //subtracting one from month since months start from 0 in javascript
}
return date;
}
var d1 = toDate(date1);
var d2 = toDate(date2);
return d1.getTime()<d2.getTime()?-1:1;
}

Thanks & Regards,
Darrel.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!