Home
Forms
Date Sorter
This script will sort an array of dates with the month spelled
out. It is done by converting the date into a more manageable
format.
The JavaScript Source: Forms: Date Sorter
Simply click inside the window below, use your cursor to highlight the script, and copy (type Control-c or Apple-c) the script into a new file in your text editor (such as Note Pad or Simple Text) and save (Control-s or Command-s). The script is yours!!!
<!-- ONE STEP TO INSTALL DATE SORTER:
1. Copy the coding into the BODY of your HTML document -->
<!-- STEP ONE: Paste this code into the BODY of your HTML document -->
<BODY>
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Original: Patrick Lewis (gtrwiz@aol.com-NOSPAM) -->
<!-- Web Site: http://www.PatrickLewis.net -->
<form name=Date>
<table border=0 width=525>
<tr><th colspan=3> Date Sorter </th></tr>
<tr><td width=175 align=middle>
Scrammbled dates<br>
<textarea cols=15 rows=10 name=Start></textarea>
</td><td align=middle width=175>
<input type=button value="Sort ascending" onclick="Sort('old')"><br><br>
<input type=button value="Sort descending" onclick="Sort('new')">
</td><td width=175 align=middle>
Sorted Dates<br>
<textarea cols=15 rows=10 name=Sorted></textarea>
</td></tr>
</table>
</form>
<script>
// #### DateSorter v1.0
// #### by Patrick Lewis
// #### www.PatrickLewis.net
// Sample Dates
StartDates = new Array(
"Sep 18, 2003",
"Jun 5, 2001",
"Aug 27, 2003",
"Dec 22, 2002",
"Aug 9, 2001",
"May 31, 2002",
"Sep 26, 2003",
"Apr 26, 2001",
"Sep 9, 2003"
);
Sign = new Array();
Sign["new"] = ">";
Sign["old"] = "<";
months=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
function ConvertDate(DT){
// Date format should be "MMM DD, YYYY"
M = DT.substring(0,DT.indexOf(" "));
D = DT.substring(DT.indexOf(" ")+1,DT.indexOf(","));
Y = DT.substring(DT.lastIndexOf(" ")+1,DT.length);
if (D.length == 1) {D = "0"+D;}
for (l=0;l<12;l++) {
if (months[l] == M) {M = l+1; break;}
}
if (M < 10) {M = "0"+M;}
return Y+""+M+""+D;
}
function Sort(How){
for (z=0;z<StartDates.length-1;z++) {
for (y=0;y<(StartDates.length-(z+1));y++) {
if ( eval(ConvertDate(StartDates[y+1])+ Sign[How] +ConvertDate(StartDates[y])) ) {
temp=StartDates[y+1];
StartDates[y+1] = StartDates[y];
StartDates[y]=temp;
}
}
}
// Load Sorted dates into textarea
document.Date.Sorted.value = "";
for (x=0;x<StartDates.length;x++) {
document.Date.Sorted.value += StartDates[x]+"\r\n";
}
}
// Load Sample dates into textarea
document.Date.Start.value = "";
for (x=0;x<StartDates.length;x++) {
document.Date.Start.value += StartDates[x]+"\r\n";
}
</script>
<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>
<!-- Script Size: 2.60 KB --> Did you use this script? Do you like this site? Please link to us!
Comments feed