Hi,
Im looking to cut down this javascript file a lot but im not quite sure how to go about it? there are a lot of repeating features in the code so im assuming its possible..... any ideas on how i could do this? as much help as possible would be appreciated as im new to the javascript game :)
var monthAbbreviations = new Array("January ", "February ", "March ", "April ", "May ", "June ", "July ", "August ", "September ", "October ", "November ", "December ");
var daysOfTheWeek = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
//TIME In milliseconds:
//MINUTES
ONEMINUTEAGO = -60000;
TWOMINUTESAGO = -120000;
THREEMINUTESAGO = -180000;
FOURMINUTESAGO = -240000;
FIVEMINUTESAGO = -300000;
SIXMINUTESAGO = -360000;
SEVENMINUTESAGO = -420000;
EIGHTMINUTESAGO = -480000;
NINEMINUTESAGO = -540000;
TENMINUTESAGO = -600000;
ELEVENMINUTESAGO = -660000;
TWELVEMINUTESAGO = -720000;
THIRTEENMINUTESAGO = -780000;
FOURTEENMINUTESAGO = -840000;
FIFTEENMINUTESAGO = -900000;
SIXTEENMINUTESAGO = -960000;
SEVENTEENMINUTESAGO = -1020000;
EIGHTEENMINUTESAGO = -1080000;
NINETEENMINUTESAGO = -1140000;
TWENTYMINUTESAGO = -1200000;
TWENTYONEMINUTESAGO = -1260000;
TWENTYTWOMINUTESAGO = -1320000;
TWENTYTHREEMINUTESAGO = -1380000;
TWENTYFOURMINUTESAGO = -1440000;
TWENTYFIVEMINUTESAGO = -1500000;
TWENTYSIXMINUTESAGO = -1560000;
TWENTYSEVENMINUTESAGO = -1620000;
TWENTYEIGHTMINUTESAGO = -1680000;
TWENTYNINEMINUTESAGO = -1740000;
THIRTYMINUTESAGO = -1800000;
THIRTYONEMINUTESAGO = -1860000;
THIRTYTWOMINUTESAGO = -1920000;
THIRTYTHREEMINUTESAGO = -1980000;
THIRTYFOURMINUTESAGO = -2040000;
THIRTYFIVEMINUTESAGO = -2100000;
THIRTYSIXMINUTESAGO = -2160000;
THIRTYSEVENMINUTESAGO = -2220000;
THIRTYEIGHTMINUTESAGO = -2280000;
THIRTYNINEMINUTESAGO = -2340000;
FOURTYMINUTESAGO = -2400000;
FOURTYONEMINUTESAGO = -2460000;
FOURTYTWOMINUTESAGO = -2520000;
FOURTYTHREEMINUTESAGO = -2580000;
FOURTYFOURMINUTESAGO = -2640000;
FOURTYFIVEMINUTESAGO = -2700000;
FOURTYSIXMINUTESAGO = -2760000;
FOURTYSEVENMINUTESAGO = -2820000;
FOURTYEIGHTMINUTESAGO = -2880000;
FOURTYNINEMINUTESAGO = -2940000;
FIFTYMINUTESAGO = -3000000;
FIFTYONEMINUTESAGO = -3060000;
FIFTYTWOMINUTESAGO = -3120000;
FIFTYTHREEMINUTESAGO = -3180000;
FIFTYFOURMINUTESAGO = -3240000;
FIFTYFIVEMINUTESAGO = -3300000;
FIFTYSIXMINUTESAGO = -3360000;
FIFTYSEVENMINUTESAGO = -3420000;
FIFTYEIGHTMINUTESAGO = -3480000;
FIFTYNINEMINUTESAGO = -3540000;
SIXTYMINUTESAGO = -3600000;
//HOURS
ONEHOURAGO = -3599999;
TWOHOURSAGO = -7200000;
THREEHOURSAGO = -10800000;
FOURHOURSAGO = -14400000;
FIVEHOURSAGO = -18000000;
SIXHOURSAGO = -21600000;
SEVENHOURSAGO = -25200000;
EIGHTHOURSAGO = -28800000;
NINEHOURSAGO = -32400000;
TENHOURSAGO = -36000000;
ELEVENHOURSAGO = -39600000;
TWELVEHOURSAGO = -43200000;
THIRTEENHOURSAGO = -46800000;
FOURTEENHOURSAGO = -46860000;
FIFTEENHOURSAGO = -46920000;
SIXTEENHOURSAGO = -46980000;
SEVENTEENHOURSAGO = -47040000;
EIGHTEENHOURSAGO = -47100000;
NINETEENHOURSAGO = -47160000;
TWENTYHOURSAGO = -47160000;
TWENTYONEHOURSAGO = -47220000;
TWENTYTWOHOURSAGO = -47280000;
TWENTYTHREEHOURSAGO = -47340000;
TWENTYFOURHOURSAGO = -47400000;
//DAYS
ONEDAYAGO = -86400000;
TWODAYSAGO = -172800000;
THREEDAYSAGO = -259200000;
FOURDAYSAGO = -345600000;
function dateReference(dateString) {
var relative = relativeDate(dateString);
var explicit = explicitDate(dateString, true);
if (relative != explicit) {
document.write("<acronym title=\"" + explicit + "\">" + relative + "</acronym>");
} else {
document.write(explicit);
}
}
function relativeDate(dateString) {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var reference = new Date(dateString);
if (isNaN(reference)) return dateString;
// Calculate time offset between the two dates
var offset = reference.getTime() - now.getTime();
// TIME AGO
//MINUTES
if ((offset > TWOMINUTESAGO) && (offset <= ONEMINUTEAGO)) return "1 Minute Ago";
if ((offset > THREEMINUTESAGO) && (offset <= TWOMINUTESAGO)) return "2 MinuteS Ago";
if ((offset > FOURMINUTESAGO) && (offset <= THREEMINUTESAGO)) return "3 Minute Ago";
if ((offset > FIVEMINUTESAGO) && (offset <= FOURMINUTESAGO)) return "4 Minutes Ago";
if ((offset > SIXMINUTESAGO) && (offset <= FIVEMINUTESAGO)) return "5 Minutes Ago";
if ((offset > SEVENMINUTESAGO) && (offset <= SIXMINUTESAGO)) return "6 Minutes Ago";
if ((offset > EIGHTMINUTESAGO) && (offset <= SEVENMINUTESAGO)) return "7 Minutes Ago";
if ((offset > NINEMINUTESAGO) && (offset <= EIGHTMINUTESAGO)) return "8 Minutes Ago";
if ((offset > TENMINUTESAGO) && (offset <= NINEMINUTESAGO)) return "9 Minutes Ago";
if ((offset > ELEVENMINUTESAGO) && (offset <= TENMINUTESAGO)) return "10 Minutes Ago";
if ((offset > TWELVEMINUTESAGO) && (offset <= ELEVENMINUTESAGO)) return "11 Minutes Ago";
if ((offset > THIRTEENMINUTESAGO) && (offset <= TWELVEMINUTESAGO)) return "12 Minutes Ago";
if ((offset > FOURTEENMINUTESAGO) && (offset <= THIRTEENMINUTESAGO)) return "13 Minutes Ago";
if ((offset > FIFTEENMINUTESAGO) && (offset <= FOURTEENMINUTESAGO)) return "14 Minutes Ago";
if ((offset > SIXTEENMINUTESAGO) && (offset <= FIFTEENMINUTESAGO)) return "15 Minutes Ago";
if ((offset > SEVENTEENMINUTESAGO) && (offset <= SIXTEENMINUTESAGO)) return "16 Minutes Ago";
if ((offset > EIGHTEENMINUTESAGO) && (offset <= SEVENTEENMINUTESAGO)) return "17 Minutes Ago";
if ((offset > NINETEENMINUTESAGO) && (offset <= EIGHTEENMINUTESAGO)) return "18 Minutes Ago";
if ((offset > TWENTYMINUTESAGO) && (offset <= NINETEENMINUTESAGO)) return "19 Minutes Ago";
if ((offset > TWENTYONEMINUTESAGO) && (offset <= TWENTYMINUTESAGO)) return "20 Minutes Ago";
if ((offset > TWENTYTWOMINUTESAGO) && (offset <= TWENTYONEMINUTESAGO)) return "21 Minutes Ago";
if ((offset > TWENTYTHREEMINUTESAGO) && (offset <= TWENTYTWOMINUTESAGO)) return "22 Minutes Ago";
if ((offset > TWENTYFOURMINUTESAGO) && (offset <= TWENTYTHREEMINUTESAGO)) return "23 Minutes Ago";
if ((offset > TWENTYFIVEMINUTESAGO) && (offset <= TWENTYFOURMINUTESAGO)) return "24 Minutes Ago";
if ((offset > TWENTYSIXMINUTESAGO) && (offset <= TWENTYFIVEMINUTESAGO)) return "25 Minutes Ago";
if ((offset > TWENTYSEVENMINUTESAGO) && (offset <= TWENTYSIXMINUTESAGO)) return "26 Minutes Ago";
if ((offset > TWENTYEIGHTMINUTESAGO) && (offset <= TWENTYSEVENMINUTESAGO)) return "27 Minutes Ago";
if ((offset > TWENTYNINEMINUTESAGO) && (offset <= TWENTYEIGHTMINUTESAGO)) return "28 Minutes Ago";
if ((offset > THIRTYMINUTESAGO) && (offset <= TWENTYNINEMINUTESAGO)) return "29 Minutes Ago";
if ((offset > THIRTYONEMINUTESAGO) && (offset <= THIRTYMINUTESAGO)) return "30 Minutes Ago";
if ((offset > THIRTYTWOMINUTESAGO) && (offset <= THIRTYONEMINUTESAGO)) return '31 Minutes Ago' ;
if ((offset > THIRTYTHREEMINUTESAGO) && (offset <= THIRTYTWOMINUTESAGO)) return '32 Minutes Ago' ;
if ((offset > THIRTYFOURMINUTESAGO) && (offset <= THIRTYTHREEMINUTESAGO)) return '33 Minutes Ago' ;
if ((offset > THIRTYFIVEMINUTESAGO) && (offset <= THIRTYFOURMINUTESAGO)) return '34 Minutes Ago' ;
if ((offset > THIRTYSIXMINUTESAGO) && (offset <= THIRTYFIVEMINUTESAGO)) return '35 Minutes Ago' ;
if ((offset > THIRTYSEVENMINUTESAGO) && (offset <= THIRTYSIXMINUTESAGO)) return '36 Minutes Ago ';
if ((offset > THIRTYEIGHTMINUTESAGO) && (offset <= THIRTYSEVENMINUTESAGO)) return '37 Minutes Ago ';
if ((offset > THIRTYNINEMINUTESAGO) && (offset <= THIRTYEIGHTMINUTESAGO)) return '38 Minutes Ago ';
if ((offset > FOURTYMINUTESAGO) && (offset <= THIRTYNINEMINUTESAGO)) return '39 Minutes Ago ';
if ((offset > FOURTYONEMINUTESAGO) && (offset <= FOURTYMINUTESAGO)) return '40 Minutes Ago ';
if ((offset > FOURTYTWOMINUTESAGO) && (offset <= FOURTYONEMINUTESAGO)) return '41 Minutes Ago ';
if ((offset > FOURTYTHREEMINUTESAGO) && (offset <= FOURTYTWOMINUTESAGO)) return '42 Minutes Ago' ;
if ((offset > FOURTYFOURMINUTESAGO) && (offset <= FOURTYTHREEMINUTESAGO)) return '43 Minutes Ago';
if ((offset > FOURTYFIVEMINUTESAGO) && (offset <= FOURTYFOURMINUTESAGO)) return '44 Minutes Ago ';
if ((offset > FOURTYSIXMINUTESAGO) && (offset <= FOURTYFIVEMINUTESAGO)) return '45 Minutes Ago' ;
if ((offset > FOURTYSEVENMINUTESAGO) && (offset <= FOURTYSIXMINUTESAGO)) return '46 Minutes Ago' ;
if ((offset > FOURTYEIGHTMINUTESAGO) && (offset <= FOURTYSEVENMINUTESAGO)) return '47 Minutes Ago ';
if ((offset > FOURTYNINEMINUTESAGO) && (offset <= FOURTYEIGHTMINUTESAGO)) return '48 Minutes Ago ';
if ((offset > FIFTYMINUTESAGO) && (offset <= FOURTYNINEMINUTESAGO)) return '49 Minutes Ago';
if ((offset > FIFTYONEMINUTESAGO) && (offset <= FIFTYMINUTESAGO)) return '50 Minutes Ago' ;
if ((offset > FIFTYTWOMINUTESAGO) && (offset <= FIFTYONEMINUTESAGO)) return '51 Minutes Ago ' ;
if ((offset > FIFTYTHREEMINUTESAGO) && (offset <= FIFTYTWOMINUTESAGO)) return '52 Minutes Ago' ;
if ((offset > FIFTYFOURMINUTESAGO) && (offset <= FIFTYTHREEMINUTESAGO)) return '53 Minutes Ago ';
if ((offset > FIFTYFIVEMINUTESAGO) && (offset <= FIFTYFOURMINUTESAGO)) return '54 Minutes Ago ';
if ((offset > FIFTYSIXMINUTESAGO) && (offset <= FIFTYFIVEMINUTESAGO)) return '55 Minutes Ago ' ;
if ((offset > FIFTYSEVENMINUTESAGO) && (offset <= FIFTYSIXMINUTESAGO)) return '56 Minutes Ago ';
if ((offset > FIFTYEIGHTMINUTESAGO) && (offset <= FIFTYSEVENMINUTESAGO)) return '57 Minutes Ago';
if ((offset > FIFTYNINEMINUTESAGO) && (offset <= FIFTYEIGHTMINUTESAGO)) return '58 Minutes Ago ';
if ((offset > SIXTYMINUTESAGO) && (offset <= FIFTYNINEMINUTESAGO)) return '59 Minutes Ago';
//HOURS
if ((offset > TWOHOURSAGO) && (offset <= ONEHOURAGO))
{
var minsOffset = Math.round(Math.abs(offset
/ 60000 + 60));
return ('One hour and '+ minsOffset + ' Minutes ago.');
}
if ((offset > THREEHOURSAGO) && (offset <= TWOHOURSAGO))
{
var minsOffset = Math.round(Math.abs(offset
/ 60000 + 60));
return ('Two hours and '+ minsOffset + ' Minutes ago.');
}
if ((offset > FOURHOURSAGO) && (offset <= THREEHOURSAGO))
{
var minsOffset = Math.round(Math.abs(offset
/ 60000 + 60));
return ('Three hours and '+ minsOffset + ' Minutes ago.');
}
if ((offset > FIVEHOURSAGO) && (offset <= FOURHOURSAGO))
{
var minsOffset = Math.round(Math.abs(offset
/ 60000 + 60));
return ('Four hours and '+ minsOffset + ' Minutes ago.');
}
if ((offset > SIXHOURSAGO) && (offset <= FIVEHOURSAGO))
{
var minsOffset = Math.round(Math.abs(offset
/ 60000 + 60));
return ('Five hours and '+ minsOffset + ' Minutes ago.');
}
if ((offset > SEVENHOURSAGO) && (offset <= SIXHOURSAGO))
{
var minsOffset = Math.round(Math.abs(offset
/ 60000 + 60));
return ('Six hours and '+ minsOffset + ' Minutes ago.');
}
if ((offset > EIGHTHOURSAGO) && (offset <= SEVENHOURSAGO))
{
var minsOffset = Math.round(Math.abs(offset
/ 60000 + 60));
return ('Seven hours and '+ minsOffset + ' Minutes ago.');
}
if ((offset > NINEHOURSAGO) && (offset <= EIGHTHOURSAGO))
{
var minsOffset = Math.round(Math.abs(offset
/ 60000 + 60));
return ('Eight hours and '+ minsOffset + ' Minutes ago.');
}
if ((offset > TENHOURSAGO) && (offset <= NINEHOURSAGO))
{
var minsOffset = Math.round(Math.abs(offset
/ 60000 + 60));
return ('Nine hours and '+ minsOffset + ' Minutes ago.');
}
if ((offset > ELEVENHOURSAGO) && (offset <= TENHOURSAGO))
{
var minsOffset = Math.round(Math.abs(offset
/ 60000 + 60));
return ('Ten hours and '+ minsOffset + ' Minutes ago.');
}
if ((offset > TWELVEHOURSAGO) && (offset <= ELEVENHOURSAGO))
{
var minsOffset = Math.round(Math.abs(offset
/ 60000 + 60));
return ('Eleven hours and '+ minsOffset + ' Minutes ago.');
}
if ((offset > THIRTEENHOURSAGO) && (offset <= TWELVEHOURSAGO)) return 'today at: '+reference;
if ((offset > FOURTEENHOURSAGO) && (offset <= THIRTEENHOURSAGO)) return 'today at:' + reference ;
if ((offset > FIFTEENHOURSAGO) && (offset <= FOURTEENHOURSAGO)) return 'today at:' + reference ;
if ((offset > SIXTEENHOURSAGO) && (offset <= FIFTEENHOURSAGO)) return 'today at:' + reference ;
if ((offset > SEVENTEENHOURSAGO) && (offset <= SIXTEENHOURSAGO)) return 'today at:' + reference ;
if ((offset > EIGHTEENHOURSAGO) && (offset <= SEVENTEENHOURSAGO)) return 'today at:' + reference ;
if ((offset > NINETEENHOURSAGO) && (offset <= EIGHTEENHOURSAGO)) return 'today at:' + reference ;
if ((offset > TWENTYHOURSAGO) && (offset <= NINETEENHOURSAGO)) return 'today at:' + reference ;
if ((offset > TWENTYONEHOURSAGO) && (offset <= TWENTYHOURSAGO)) return 'today at:' + reference ;
if ((offset > TWENTYTWOHOURSAGO) && (offset <= TWENTYONEHOURSAGO)) return 'today at:' + reference ;
if ((offset > TWENTYTHREEHOURSAGO) && (offset <= TWENTYTWOHOURSAGO)) return 'today at:' + reference ;
if ((offset > TWENTYFOURHOURSAGO) && (offset <= TWENTYTHREEHOURSAGO)) return 'today at:' + reference ;
//Days
if ((offset > TWODAYSAGO) && (offset <= ONEDAYAGO)) return "1 Day Ago";
if ((offset > THREEDAYSAGO) && (offset <= TWODAYSAGO)) return "2 Days Ago";
if ((offset > FOURDAYSAGO) && (offset <=THREEDAYSAGO)) return "3 Days Ago";
// None of the above
return explicitDate(dateString, false);
}
function explicitDate(dateString, includeYearRegardless) {
var now = new Date();
var reference = new Date(dateString);
if (isNaN(reference)) return dateString;
var date = reference.getDate();
var month = monthAbbreviations[referen
ce.getMont
h()];
var hours = reference.getHours();
var minutes = reference.getMinutes();
var seconds = reference.getSeconds();
var year = reference.getFullYear();
// Only show the year if out of current year!!!!
/* if ((reference.getFullYear() != now.getFullYear()) || includeYearRegardless) {
var year = ", " + reference.getFullYear();
} else {
var year = "";
}*/
return(month + date + ', ' + year + ' at ' + hours +':'+ minutes);
}
thanks!