Thành viên:Trần Nguyễn Minh Huy/Dates.js
Giao diện
Sau khi lưu trang này, hãy xóa bộ nhớ đệm của trình duyệt để những thay đổi hiện ra:
Chrome, Firefox, Internet Explorer, Opera: Ctrl+⇧ Shift+R
//<pre><nowiki>
var months_key = new Array('tháng 1','tháng 2','tháng 3','tháng 4','tháng 5','tháng 6','tháng 7','tháng 8','tháng 9','tháng 10','tháng 11','tháng 12');
// 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)
{
if (date.search(/\d{4}/) !=-1)
var Year = date.match(/\d{4}/)[0];
else
alert('[retrieveYear] Lỗi tại: ' + date);
return Year;
}
// Returns 1-12
function retrieveMonth(date)
{
if (date.search(/-\d{1,2}-/) !=-1)
{var Month = date.match(/-\d{1,2}-/)[0].replace(/-/g,''); return frmtDateNum(Month);}
for (var i=0; i<months_key.length; i++)
{
if (date.search(months_key[i]) !=-1)
{Month = i+1; break;}
//else if (date.search(months_key[i].substring(0,3)) !=-1)
// {Month = i+1; break;}
}
return Month;
}
// Returns 1-31
function retrieveDay(date)
{
if (date.search(/[ngày ,-]\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;
//////
//</nowiki></pre>