User:Voice of All/Dates.js

//

var months_key = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

// Will accurately parse the time difference between two dates (years after 1970)

// EXCEPTED formats (all UTC):

// 16:12, January 15, 2001

// 16:12, 15 January 2001

// 16:12, 2001 January 15

// 16:12, Jan 15, 2001

// 16:12, 15 Jan 2001

// 16:12, 2001 Jan 15

// 2001-01-15T16:12:34 <--seconds ignored

// Fri, 10 Nov 2006 17:39

function getElapsedTime(top_date,bottom_date)

{

// Get top year

var TYear = retrieveYear(top_date);

// Get lower year

var BYear = retrieveYear(bottom_date);

// Get top month

var TMonth = retrieveMonth(top_date);

// Get lower month

var BMonth = retrieveMonth(bottom_date);

// Get top date

var TDay = retrieveDay(top_date);

// Get lower date

var BDay = retrieveDay(bottom_date);

// Get top time

var TTime = retrieveTime(top_date).split(':');

// Get lower time

var BTime = retrieveTime(bottom_date).split(':');

// Get date in ms from start of year 1971

var rtime = Date.UTC(TYear,TMonth-1,TDay,frmtDateNum(TTime[0]),frmtDateNum(TTime[1]),0);

var ltime = Date.UTC(BYear,BMonth-1,BDay,frmtDateNum(BTime[0]),frmtDateNum(BTime[1]),0);

// Get difference and convert to days

var day = 24*60*60*1000; // Days in ms

return (rtime - ltime)/day;

}

// Will accurately parse the time difference between two dates (years after 0)

// counts leap years and febuary month length changes

// EXCEPTED formats:

// 16:12, January 15, 2001

// 16:12, 15 January 2001

// 16:12, 2001 January 15

// 16:12, Jan 15, 2001

// 16:12, 15 Jan 2001

// 16:12, 2001 Jan 15

// 2001-01-15T16:12:34 <--seconds ignored

// Fri, 10 Nov 2006 17:39

function getDateDiff(top_date,bottom_date)

{

// Get top year

var TYear = retrieveYear(top_date);

// Get leap year factor

var TFEB = getFebDays(TYear);

// Get lower year

var BYear = retrieveYear(bottom_date);

// Get leap year factor

var BFEB = getFebDays(BYear);

var months_tvalue = new Array(31, TFEB, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

var months_bvalue = new Array(31, BFEB, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

// Get top month

var TMonth = retrieveMonth(top_date);

// Get lower month

var BMonth = retrieveMonth(bottom_date);

// Get top date

var TDay = retrieveDay(top_date);

// Get lower date

var BDay = retrieveDay(bottom_date);

// Get top time and turn it into days

var TTime = convTimeToDays(retrieveTime(top_date));

// Get lower time and turn it into days

var BTime = convTimeToDays(retrieveTime(bottom_date));

// Parse, months are 1-12, but array starts at 0, so take 1 one the index

// monthr and monthl get amount of relavent days passed in the recent and old month respectively

var monthr = TDay - 1 + TTime;

var monthl = months_bvalue[BMonth-1] - (BDay - 1 + BTime);

if (TYear == BYear)

{

var mdiff = addFullMonths(TMonth-1,BMonth+1,months_tvalue);

var totdays = monthr + monthl + mdiff;

if (TMonth == BMonth)

{totdays = monthr - (BDay - 1 + BTime);}

}

else

{

var ydiff = addFullYears(TYear - 1,BYear + 1);

var yearr = addFullMonths(TMonth - 1,1,months_tvalue) + monthr;

var yearl = addFullMonths(12,BMonth + 1,months_bvalue) + monthl;

var totdays = yearr + ydiff + yearl;

}

return totdays;

}

function addFullYears(T,B)

{

var tot_days = 0;

for (var i=T; i>=B; i--)

{

FEB = getFebDays(i);

if (FEB == 28)

tot_days += 365;

else if (FEB == 29)

tot_days += 366;

}

return tot_days;

}

//3rd var is the calendar (for Leap Years)

function addFullMonths(T,B,months_value)

{

var tot_days = 0;

for (var i=T; i>=B; i--)

{tot_days += months_value[i-1];}

return tot_days;

}

function retrieveYear(date)

{

var Year = '0000';

if (date.search(/\d{4}/) !=-1)

Year = date.match(/\d{4}/)[0];

else

alert('[retrieveYear] Error at: ' + date);

return Year;

}

// Returns 1-12

function retrieveMonth(date)

{

var Month = 0;

if (date.search(/-\d{1,2}-/) !=-1)

{Month = date.match(/-\d{1,2}-/)[0].replace(/-/g,''); return frmtDateNum(Month);}

for (var i=0; i

{

if (date.search(months_key[i]) !=-1 || date.search(months_key[i].substring(0,3)) !=-1)

{Month = i+1; break;}

}

return Month;

}

// Returns 1-31

function retrieveDay(date)

{

if (date.search(/[ ,-]\d{1,2}([ ,T]|$)/) !=-1)

var Days = date.match(/[ ,-]\d{1,2}([ ,T]|$)/)[0].replace(/[-, T]/g,'');

else

alert('[retrieveDay] Error at: ' + date);

Days = frmtDateNum(Days);

return Days;

}

function retrieveTime(date)

{

if (date.search(/^\d\d:\d\d,/) !=-1)

var Time = date.match(/^\d\d:\d\d,/)[0];

else if (date.search(/T\d\d:\d\d/) !=-1)

var Time = date.match(/T\d\d:\d\d/)[0];

else if (date.search(/ \d\d:\d\d$/) !=-1)

var Time = date.match(/ \d\d:\d\d$/)[0];

else if (date.search(/ \d\d:\d\d:/) !=-1)

var Time = date.match(/ \d\d:\d\d:/)[0];

else alert('[retrieveTime] Error at: ' + date);

Time = Time.replace(/[ T,]|:$/g,'');

return Time;

}

function getFebDays(Year)

{

var LP4 = Year/4;

var LP100 = Year/100;

var LP400 = Year/400;

if (Math.round(LP4) == LP4 && (Math.round(LP100) != LP100 || Math.round(LP400) == LP400))

var FEB = 29;

else

var FEB = 28;

return FEB;

}

//remove pointless zeros

function frmtDateNum(number)

{

number = new String(number);

x = (number=='0') ? 0 : number.replace(/^0+/g,'');

return x;

}

//converts a time into days

function convTimeToDays(Time)

{

var mins = frmtDateNum(Time.split(':')[1]); var hours = frmtDateNum(Time.split(':')[0]);

TimeDays = (hours + (mins/60))/24;

return TimeDays;

}

// 16:12, January 15, 2001

// 16:12, Jan 15, 2001

// 16:12, 15 January 2001

// 16:12, 15 Jan 2001

// 16:12, 2001 January 15

// 16:12, 2001 Jan 15

// 2001-01-15T16:12

// Fri, 10 Nov 2006 17:39

var STAND_DATE_REGEXP = /\d{1,2}:\d\d, [^ ]+ \d{1,2}, \d\d\d\d|\d{1,2}:\d\d, \d{1,2} [^ ]+ \d\d\d\d|\d{1,2}:\d\d,\d\d\d\d [^ ]+ \d{1,2}|\d\d\d\d-\d\d-\d\dT\d{1,2}:\d\d|[^ ]+, \d{1,2} [^ ]+ \d\d\d\d \d{1,2}:\d\d/g;

//////

//