In order to sort on a date format which is not automatically detected by Date().parse() you will need to define a custom sorting function to convert the string (the date) into something which can be sorted numerically. And example of using this plug-in method can be seen here:
For the format dd/mm/YYYY you can probably do something as simple as:
[code]
/* if x is in the format dd/mm/YYYY */
var ax = x.split(/\//);
var a = ( ax[2]+ax[1]+ax[0] ) * 1;
[/code]
Then you have a number which can be sorted on. Sticking this into a sorting function just need to do this formatting for both of the sort inputs and then compare them. Have a look at the DataTables internal numeric sorting functions, that in combination with this might help.
Hi Allan,
I have a quesstion about the date sorting how do i know what will be detected by the Date().parse() method?
I have some dates in a format like so: May 2 2009 12:00PM
Is there a better format that I could use so that datagrids would recognize this date format?
Hi Allan,
thanks for the quick response. I figured out what was causing the trouble. Date.parse doesn't like the AM/PM at the end of the date string, and that prevents it from sorting the dates correctly.
Would you know how to make DataTables ignore the am pm at the end of the datestring? I would put it into 24hour time, but my users (saleswomen who find word difficult and confusing) wouldn't like that.
What to do here is create a custom sorting function which will parse your date string into something which can be sorted (Date.parse converts to a integer for example). How this can be done is shown in this example: http://datatables.net/examples/example_sorting_plugin.html
In your case either you can try and parse the date directly, or perhaps strip the AM/PM (adding 12 hours as approbate).
Another option is to have a hidden column in the table which has the integer representation of the time, such that the visible column is the friendly "human" one, but is sorted based upon the hidden column. See http://datatables.net/usage#iDataSort for this.
Thank you for your suggestions! I think i'm going to cop out and take the easy road out and create a separate column called Event Time and set the sorting for that column to null. :-p.
Hi Allan
I have a dateformat configuration, i set the configuration on the fly sometimes it is dd/mm/yyyy,mm/dd/yyyy and separated with / or . how to sort the that kind of dateformats dynamically and some times my datecolumn will vary from different locations some times that would be 4 th column and sometimes 5,6, e., in this case how to mention in aoColumn please suggest me in this reagards
Replies
Thanks for the kind words about DataTables!
In order to sort on a date format which is not automatically detected by Date().parse() you will need to define a custom sorting function to convert the string (the date) into something which can be sorted numerically. And example of using this plug-in method can be seen here:
http://datatables.net/examples/example_sorting_plugin.html
If you need a hand with writing a sorting function for your date string, let us know!
Regards,
Allan
ffdez, did you get the solution? could you share it, please?
I'm not used to js, so any help with this would be appreciated.
Regards,
roger
[code]
/* if x is in the format dd/mm/YYYY */
var ax = x.split(/\//);
var a = ( ax[2]+ax[1]+ax[0] ) * 1;
[/code]
Then you have a number which can be sorted on. Sticking this into a sorting function just need to do this formatting for both of the sort inputs and then compare them. Have a look at the DataTables internal numeric sorting functions, that in combination with this might help.
Regards,
Allan
thanks a lot,
roger
I have a quesstion about the date sorting how do i know what will be detected by the Date().parse() method?
I have some dates in a format like so: May 2 2009 12:00PM
Is there a better format that I could use so that datagrids would recognize this date format?
It's worth taking a look at the MDC page for Date.parse - https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date/parse
I just tried your date string in Firefox with Firebug and it doesn't parse as one would hope. The formats on the MDC page are probably your best best.
Allan
thanks for the quick response. I figured out what was causing the trouble. Date.parse doesn't like the AM/PM at the end of the date string, and that prevents it from sorting the dates correctly.
Would you know how to make DataTables ignore the am pm at the end of the datestring? I would put it into 24hour time, but my users (saleswomen who find word difficult and confusing) wouldn't like that.
Thanks,
Steve
What to do here is create a custom sorting function which will parse your date string into something which can be sorted (Date.parse converts to a integer for example). How this can be done is shown in this example: http://datatables.net/examples/example_sorting_plugin.html
In your case either you can try and parse the date directly, or perhaps strip the AM/PM (adding 12 hours as approbate).
Another option is to have a hidden column in the table which has the integer representation of the time, such that the visible column is the friendly "human" one, but is sorted based upon the hidden column. See http://datatables.net/usage#iDataSort for this.
Hope this helps,
Allan
I have a dateformat configuration, i set the configuration on the fly sometimes it is dd/mm/yyyy,mm/dd/yyyy and separated with / or . how to sort the that kind of dateformats dynamically and some times my datecolumn will vary from different locations some times that would be 4 th column and sometimes 5,6, e., in this case how to mention in aoColumn please suggest me in this reagards
Sudhakar