Click to See Complete Forum and Search --> : Sorting dates


Webskater
09-25-2003, 02:51 PM
I want to do some client side sorting of columns in a table. One of the columns contains dates in shortdate format and in the English format i.e. 31/01/03
As far as I can see javascript only sees dates as being in US format. Can anyone give me some guidance on how I can get a function to sort the dates in a column so that if you click on a column header it reverses the sort order of the dates in the column below.
Thanks for any help.

Charles
09-25-2003, 03:09 PM
<script type="text/javascript">
<!--
Date.fromEDF = function (d) {var a = d.split('/'); return new Date (a[2] < 100 ? Number (a[2]) + 2000 : a[2], ++a[1], a[0])}

alert(['9/5/03', '5/9/04', '1/1/00'].sort(function (a,b) {a = Date.fromEDF(a).getTime(); b = Date.fromEDF(b).getTime(); if (a < b) return -1; if (a == b) return 0; if (a > b) return 1}))
// -->
</script>

Webskater
09-26-2003, 04:57 AM
Charles, thank you very much for your answer. I only wish I understood some of it.
I always create a function so:
function getdate(d)
whereas you have used:
Date.fromEDF = function (d)
How do you call a function when you create it the way you do?
Also, could you tell me how to use the function so that if I click on the word "Date" in the table shown below, it will reverse the sort order of the dates. Thanks again.
<table>
<tr>
<td>Date</td>
</tr>
<tr>
<td>31/01/2003</td>
</tr>
<tr>
<td>05/04/2003</td>
</tr>
<tr>
<td>20/09/2003</td>
</tr>
</table>

Webskater
09-26-2003, 05:28 AM
Hello again Charles. Having had a go at trying to get your code to work I see it puts an alert on the screen showing the dates '9/5/03', '5/9/04', '1/1/00' reversed.
If I put dates like this into the code
'31/1/03', '25/2/03', '19/3/03'
it does not resort them. Can this work for dates in English format?
Thanks again.

Charles
09-26-2003, 06:08 AM
Originally posted by Webskater
Hello again Charles. Having had a go at trying to get your code to work I see it puts an alert on the screen showing the dates '9/5/03', '5/9/04', '1/1/00' reversed.
If I put dates like this into the code
'31/1/03', '25/2/03', '19/3/03'
it does not resort them. Can this work for dates in English format?
Thanks again. If by "English format" you mean "dd/mm/yy" then "'31/1/03', '25/2/03', '19/3/03'" is already sorted properly.

You call my method of the Date class thusly: Date.fromEDF('19/3/03') except that I have writen it so that you can use four diget years. Haven't you learned anything from Y2K?

Functions in JavaScript are really methods of the Window object and it made more sense to make a method of the Date class.

Webskater
09-26-2003, 06:41 AM
What I was trying to achieve was not sorting dates in the correct order, I want to be able to sort and resort the dates in a column in a table so that a user can click the column heading to resort the dates - either most recent date first or most recent date last.
The dates are displayed on screen in English short date format because the application is being used here and there is not much room on the screen. With regard to Y2K - I have learned that the only reliable way of dealing with dates is letting the database handle them the way it wants (which for SQL Server is US format) and then displaying them on screen using functions that do not rely on the regional settings of the computer. I do in fact use 4 digit years - I showed 2 digit years in my example because there is never a problem sorting years - it always occurs because of the difference between the dd/mm and mm/dd on either side of the Atlantic.