This script sorts a list of dates into order - old -> new
Can be used for dates which do not exist yet!
To use this simply use the code:
ago(VAR1, new Array(DATE1), new Array(DATE2));
Adding as many dates within arrays and seperated by commas as you like.
VAR1 = the date which you want the script to return:
e.g. 0 = oldest date
1 = second oldest date
2 = third oldest date
etc.
DATE1 = must be in the following format within the array:
new Array(1, 2, 3, 4, 5)
1 = minutes of the hour
2 = hours of the day
3 = date of the month
4 = month of the year
5 = year
You can have as many dates as you like.
Example of code use:
alert(ago(1, new Array(1, 12, 29, 0, 2004), new Array(13,4,29,0,2005), new Array(24, 15, 13, 11, 9011), new Array(25, 11, 16, 10, 1456)));
function ago()
{
var x = 0; date = new Array(); y = 0; returns = new Array();
while (x < arguments.length)
{
if (typeof(arguments[x]) != "number")
{
var curArgument = arguments[x];
date[x] = new Date();
date[x].setMinutes(curArgument[0]);
date[x].setHours(curArgument[1]);
date[x].setDate(curArgument[2]);
date[x].setMonth(curArgument[3]);
date[x].setFullYear(curArgument[4]);
}
x++;
}
date.valueOf().sort(function(a,b)
{
return a - b;
});
return (typeof(arguments[0]) == "number") ? date[arguments[0]] : date;
}
I would usually either cut out the time section and use it to work out who's older or I would use it to order a group of events, e.g.:
User1 Logged In
User2 Posted A Comment
As a rough example. To be honest, I was really just making it for fun and to experiment with not limited variables passed to the function.