User:Ed Poor/dateutil.js

function datePart(yourDate) {

// Convert parameter to Date object (includes time part) myDate = new Date(yourDate); // Break it into year, month, day. Then put it // back togther to get the date part, without time. myYear = dateToYear(myDate) myMonth = myDate.getMonth + 1; myDay = myDate.getDate;	// getDate returns day of month! dateString = myMonth + "/" + myDay + "/" + myYear; return new Date(dateString); }

function debug(blurb) { document.write("" + blurb + ""); }

// Return the whole number of days between two dates. function daysBetween(date1, date2) {	var firstDate = datePart(date1); var secondDate = datePart(date2);

var milliseconds1 = firstDate.getTime; var milliseconds2 = secondDate.getTime; var difference = (milliseconds2 - milliseconds1); var millisecondsPerDay = 1000 * 3600 * 24; var rawDays = difference / millisecondsPerDay; var fudgeFactor = 0.05; var daysDifference = Math.floor(fudgeFactor + difference / millisecondsPerDay);

return daysDifference; }

// Returns an English phrase // telling how many days remain before a date. // Returns empty string if negative parameter passed. // Example: //	0	today //	1	tomorrow //	2	in 2 days //	11	in 1 1/2 weeks //	14	in 2 weeks function daysRemaining(dayCount) {

var dayLimit = 10; var halfWeekLimit = 42; var fullWeekLimit = 70; // Ignore negative numbers. if (dayCount < 0) { return ""; }	// A number of days. if (dayCount == 0) { return "today"; } else if (dayCount == 1) { return "tomorrow"; } else if (dayCount <= dayLimit) { return "in " + dayCount + " days"; }	// A number of weeks. weekCount = dayCount / 7; if (dayCount <= halfWeekLimit) { return "in " + nearestHalf(weekCount) + " weeks"; } else if (dayCount <= fullWeekLimit) { return "in " + weekCount + " weeks"; } }

function daysToAnniversary(originaldate) { today = new Date; thisyear = dateToYear(today);

target = new Date(originaldate); targetmonth = target.getMonth + 1; targetday = target.getDate; anniversaryString = targetmonth + "/" + targetday + "/" + thisyear; anniversary = new Date(anniversaryString); if (anniversary < today) { anniversaryString = targetmonth + "/" + targetday + "/" + (thisyear + 1); }

var days = daysBetween(today, anniversaryString); return days; }

function ageString(birthdate, daysInAdvance) { yearsOld = ageinyears(birthdate); daysLeft = daysToAnniversary(birthdate); age = nearestQuarter(yearsOld + (365 - daysLeft) / 365) + " years old"; if (daysLeft <= daysInAdvance) { age = age + " (birthday in " + daysLeft + " days)"; }	return age; }

// arg must be positive number function nearestHalf(floatnum) {

var whole = Math.floor(floatnum); var fraction = floatnum - whole;

var halfCount = Math.floor(fraction * 2);

switch (halfCount) { case 0: return whole; case 1: return whole + "&frac12;"; } }

// arg must be positive number function nearestQuarter(floatnum) {

var whole = Math.floor(floatnum); var fraction = floatnum - whole;

var quarterCount = Math.floor(fraction * 4);

switch (quarterCount) { case 0: return whole; case 1: return whole + "&frac14;"; case 2: return whole + "&frac12;"; case 3: return whole + "&frac34;"; } }

function periodBetween(date1, date2) // Returns whole number of days between two dates. // Bug: Appears to have a one-off error. // Constraint: date1 < date2 {	var period; var firstDate = datePart(date1); var secondDate = datePart(date2);

var milliseconds1 = firstDate.getTime; var milliseconds2 = secondDate.getTime; var difference = milliseconds2 - milliseconds1; var millisecondsPerDay = 1000.0 * 3600.0 * 24.0;

var daysDifference = Math.floor(difference/millisecondsPerDay - 5); var weeksDifference = Math.floor(daysDifference / 7);

// Days if (daysDifference == 0) { dayPeriod = "today"; } else if (daysDifference == 1) { dayPeriod = "tomorrow"; } else { dayPeriod = "in " + daysDifference + " days"; }

// Nearabouts var nearly = ""; var approx = daysDifference % 7; if (approx == 1) { nearly = "just over "; } else if (approx == 2) { nearly = "more than " } else if (approx == 6) { nearly = "less than " } else if (approx == 7) { nearly = "just under " }	// Weeks if (weeksDifference == 1) { if (daysDifference == 10 || daysDifference == 11) { weekPeriod = "in a week and a half"; } else { weekPeriod = "in a week"; }	} else { weekPeriod = "in " + nearly + weeksDifference + " weeks"; }	if (daysDifference <= 6) { return dayPeriod; } else { return weekPeriod; } }

function dateToYear(theDate) {	theyear = theDate.getYear; if (theyear < 1800) { theyear = theyear + 1900; }	return theyear }

function todaysdate {	today = new Date;

thismonth = today.getMonth + 1; thisday = today.getDate; thisyear = dateToYear(today);

return thismonth + '/' + thisday + '/' + thisyear; }

// CALCULATE AGE IN YEARS -- THE AGE INCREMENTS ANNUALLY, PRECISELY ON YOUR BIRTHDAY function ageinyears(yourbirthdate) {

today = new Date; thisyear = dateToYear(today); thismonth = today.getMonth + 1;	// 0=Jan, 1=Feb, etc. thisday = today.getDate;		// Day of month

target = new Date(yourbirthdate); targetyear = dateToYear(target); targetmonth = target.getMonth + 1; targetday = target.getDate;

yeardiff = thisyear - targetyear; monthdiff = thismonth - targetmonth; daydiff = thisday - targetday;

// Decrement yeardiff if today's month & day are earlier than target's. if ( monthdiff < 0 || (monthdiff == 0 && daydiff < 0) ) {	yeardiff = yeardiff - 1; }

return yeardiff; }