function getDateLatest()
{
var date0 = new Date('12/31/2005');
var date1 = new Date('11/29/2007');
var date2 = new Date('01/13/1999');
var dateArray = new Array();
//we store date as time in milliseconds.
//getTime() returns the number of milliseconds since
//midnight of January 1, 1970. This method is also very
//useful for generating a unique sequence of numbers.
dateArray[0] = date0.getTime();
dateArray[1] = date1.getTime();
dateArray[2] = date2.getTime();
dateArray.sort(sortNumber);
var dateLatest = new Date(dateArray[dateArray.length - 1]);
alert(dateLatest);}
//By default, Array sorts alphabetically. To get it to sort
//numerically, we write a function that compares numbers.
function sortNumber(a, b)
{
return a - b
}
Sunday, January 22, 2006
javascript: Sorting Date in an Array (Collection)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment