The moose likes HTML, CSS and JavaScript and the fly likes problem with date fields  Table sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "problem with date fields  Table sorting " Watch "problem with date fields  Table sorting " New topic

problem with date fields Table sorting

sirisha makkapati
Ranch Hand

Joined: Dec 10, 2006
Posts: 83
Hello friends,
I am doing table sorting with javascript.
I wanted to sort date fields.
can any body give me an idea how to add code to sort date fields to my scipt.

My script is as follows
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="" xml:lang="en" lang="en">


Thanks in advance.

<script language="JavaScript" type="text/javascript">

function zxcSortTable(zxcid,zxccnus){
var zxcargs=zxcSortTable.arguments
var zxctable=document.getElementById(zxcid);
var zxcrows=zxctable.rows,zxcscells=[],zxcclones=[],zxcblanks=[],zxccnt=0;
for (var zxc0=1;zxc0<zxcargs.length;zxc0++){
for (var zxc0a=1;zxc0a<zxcrows.length;zxc0a++){
var zxccells=zxcrows[zxc0a].cells;
var zxcdata=zxccells[zxcargs[zxc0]];
if (/[a-z]|[0-9]/i.test(zxcdata)){
if (!zxcrows[0].cells[zxcargs[1]].ud) zxcrows[0].cells[zxcargs[1]].ud='Up';
if (zxcrows[0].cells[zxcargs[1]].ud=='Up') zxcclones=zxcclones.reverse();
for (var zxc1=0;zxc1<zxcclones.length;zxc1++){
for (var zxc2=1;zxc2<zxcargs.length;zxc2++){

function zxcSortTxt(zxca,zxcb){
if (zxcaa>zxcbb) return 1;
if (zxcaa<zxcbb) return -1;
return 0;

Thanks in advance.
Bob Good
Ranch Hand

Joined: Jan 09, 2008
Posts: 86
I think you need to turn you dates into a yyyy-mm-dd string that will sort properly like this:

da = new Date(document.lastModified) // Create a Date Object set to the last modifed date
dy = da.getFullYear() // Get full year (as opposed to last two digits only)
dm = da.getMonth() + 1 // Get month and correct it (getMonth() returns 0 to 11)
dd = da.getDate() // Get date within month
if ( dy < 1970 ) dy = dy + 100; // We still have to fix the millennium bug
ys = new String(dy) // Convert year, month and date to strings
ms = new String(dm)
ds = new String(dd)
if ( ms.length == 1 ) ms = "0" + ms; // Add leading zeros to month and date if required
if ( ds.length == 1 ) ds = "0" + ds;
ys = ys + "-" + ms + "-" + ds // Combine year, month and date in ISO format
document.write ( "Last Updated " + ys ) // Display the result

Thanks to
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
You would have to change the zxcSortTxt.

Here is the basic idea, you would have to figure out how to apply it:

[ January 22, 2008: Message edited by: Eric Pascarello ]
I agree. Here's the link:
subject: problem with date fields Table sorting
jQuery in Action, 3rd edition