__________________
Senast redigerad av e7andy 2018-03-03 kl. 23:43.
Senast redigerad av e7andy 2018-03-03 kl. 23:43.
function yearDiff(d1, d2) {
years = d2.getFullYear() - d1.getFullYear();
months = d2.getMonth() - d1.getMonth();
return months < 0 ? years - 1 : years;
}
function monthDiff(d1, d2) {
months = d2.getMonth() - d1.getMonth();
days = d2.getDate() - d1.getDate();
months = months < 0 ? months + 12 : months;
return days < 0 ? months - 1 : months;
}
function dayDiff(d1, d2) {
days = d2.getDate() - d1.getDate();
if(days < 0) {
days1 = new Date(d1.getYear(), d1.getMonth()+1, 0).getDate();
days = days1 - d1.getDate() + d2.getDate();
}
hours = d2.getHours() - d1.getHours();
return hours < 0 ? days - 1 : days;
}
function hourDiff(d1, d2) {
hours = d2.getHours() - d1.getHours();
minutes = d2.getMinutes() - d1.getMinutes();
hours = hours < 0 ? hours + 24 : hours;
return minutes < 0 ? hours - 1 : hours;
}
function minuteDiff(d1, d2) {
minutes = d2.getMinutes() - d1.getMinutes();
seconds = d2.getSeconds() - d1.getSeconds();
minutes = minutes < 0 ? minutes + 60 : minutes;
return seconds < 0 ? minutes - 1 : minutes;
}
function secondDiff(d1, d2) {
seconds = d2.getSeconds() - d1.getSeconds();
return seconds < 0 ? seconds + 60 : seconds;
}
date1 = new Date("2017-03-19T00:01:01")
date2 = new Date("2018-01-20T00:02:01")
console.log("Years: " + yearDiff(date1, date2));
console.log("Months: " + monthDiff(date1, date2));
console.log("Days: " + dayDiff(date1, date2));
console.log("Hours: " + hourDiff(date1, date2));
console.log("Minutes: " + minuteDiff(date1, date2));
console.log("Seconds: " + secondDiff(date1, date2));
function yearDiff(d1, d2) {
years = d2.getFullYear() - d1.getFullYear();
months = d2.getMonth() - d1.getMonth();
return months < 0 ? years - 1 : years;
}
function monthDiff(d1, d2) {
months = d2.getMonth() - d1.getMonth();
days = d2.getDate() - d1.getDate();
months = months < 0 ? months + 12 : months;
return days < 0 ? months - 1 : months;
}
function dayDiff(d1, d2) {
days = d2.getDate() - d1.getDate();
if(days < 0) {
days1 = new Date(d1.getYear(), d1.getMonth()+1, 0).getDate();
days = days1 - d1.getDate() + d2.getDate();
}
hours = d2.getHours() - d1.getHours();
return hours < 0 ? days - 1 : days;
}
function hourDiff(d1, d2) {
hours = d2.getHours() - d1.getHours();
minutes = d2.getMinutes() - d1.getMinutes();
hours = hours < 0 ? hours + 24 : hours;
return minutes < 0 ? hours - 1 : hours;
}
function minuteDiff(d1, d2) {
minutes = d2.getMinutes() - d1.getMinutes();
seconds = d2.getSeconds() - d1.getSeconds();
minutes = minutes < 0 ? minutes + 60 : minutes;
return seconds < 0 ? minutes - 1 : minutes;
}
function secondDiff(d1, d2) {
seconds = d2.getSeconds() - d1.getSeconds();
return seconds < 0 ? seconds + 60 : seconds;
}
date1 = new Date("2017-03-19T00:01:01")
date2 = new Date("2018-01-20T00:02:01")
console.log("Years: " + yearDiff(date1, date2));
console.log("Months: " + monthDiff(date1, date2));
console.log("Days: " + dayDiff(date1, date2));
console.log("Hours: " + hourDiff(date1, date2));
console.log("Minutes: " + minuteDiff(date1, date2));
console.log("Seconds: " + secondDiff(date1, date2));
function yearDiff(d1, d2) {
years = d2.getFullYear() - d1.getFullYear();
months = d2.getMonth() - d1.getMonth();
return months < 0 ? years - 1 : years;
}
function monthDiff(d1, d2) {
months = d2.getMonth() - d1.getMonth();
days = d2.getDate() - d1.getDate();
months = months < 0 ? months + 12 : months;
return days < 0 ? months - 1 : months;
}
function dayDiff(d1, d2) {
days = d2.getDate() - d1.getDate();
if(days < 0) {
days1 = new Date(d1.getYear(), d1.getMonth()+1, 0).getDate();
days = days1 - d1.getDate() + d2.getDate();
}
hours = d2.getHours() - d1.getHours();
return hours < 0 ? days - 1 : days;
}
function hourDiff(d1, d2) {
hours = d2.getHours() - d1.getHours();
minutes = d2.getMinutes() - d1.getMinutes();
hours = hours < 0 ? hours + 24 : hours;
return minutes < 0 ? hours - 1 : hours;
}
function minuteDiff(d1, d2) {
minutes = d2.getMinutes() - d1.getMinutes();
seconds = d2.getSeconds() - d1.getSeconds();
minutes = minutes < 0 ? minutes + 60 : minutes;
return seconds < 0 ? minutes - 1 : minutes;
}
function secondDiff(d1, d2) {
seconds = d2.getSeconds() - d1.getSeconds();
return seconds < 0 ? seconds + 60 : seconds;
}
date1 = new Date("2017-03-19T00:01:01")
date2 = new Date("2018-01-20T00:02:01")
console.log("Years: " + yearDiff(date1, date2));
console.log("Months: " + monthDiff(date1, date2));
console.log("Days: " + dayDiff(date1, date2));
console.log("Hours: " + hourDiff(date1, date2));
console.log("Minutes: " + minuteDiff(date1, date2));
console.log("Seconds: " + secondDiff(date1, date2));
function yearDiff(d1, d2) {
years = d2.getFullYear() - d1.getFullYear();
months = d2.getMonth() - d1.getMonth();
return months < 0 ? years - 1 : years;
}
function monthDiff(d1, d2) {
months = d2.getMonth() - d1.getMonth();
days = d2.getDate() - d1.getDate();
months = months < 0 ? months + 12 : months;
return days < 0 ? months - 1 : months;
}
function dayDiff(d1, d2) {
days = d2.getDate() - d1.getDate();
if(days < 0) {
days1 = new Date(d1.getYear(), d1.getMonth()+1, 0).getDate();
days = days1 - d1.getDate() + d2.getDate();
}
hours = d2.getHours() - d1.getHours();
return hours < 0 ? days - 1 : days;
}
function hourDiff(d1, d2) {
hours = d2.getHours() - d1.getHours();
minutes = d2.getMinutes() - d1.getMinutes();
hours = hours < 0 ? hours + 24 : hours;
return minutes < 0 ? hours - 1 : hours;
}
function minuteDiff(d1, d2) {
minutes = d2.getMinutes() - d1.getMinutes();
seconds = d2.getSeconds() - d1.getSeconds();
minutes = minutes < 0 ? minutes + 60 : minutes;
return seconds < 0 ? minutes - 1 : minutes;
}
function secondDiff(d1, d2) {
seconds = d2.getSeconds() - d1.getSeconds();
return seconds < 0 ? seconds + 60 : seconds;
}
date1 = new Date("2017-03-19T00:01:01")
date2 = new Date("2018-01-20T00:02:01")
console.log("Years: " + yearDiff(date1, date2));
console.log("Months: " + monthDiff(date1, date2));
console.log("Days: " + dayDiff(date1, date2));
console.log("Hours: " + hourDiff(date1, date2));
console.log("Minutes: " + minuteDiff(date1, date2));
console.log("Seconds: " + secondDiff(date1, date2));
Years: 0 Months: 1 Days: 3 Hours: 0 Minutes: 0 Seconds: 0
Years: 0 Months: 1 Days: 3 Hours: 0 Minutes: 0 Seconds: 0
... 6 awk ' 7 BEGIN { 8 oLymp = 1 + 4 * 365; 9 mnams = \ 10 "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"; 11 numms = "1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12"; 12 mday ="31 28 31 30 31 30 31 31 30 31 30 31" 13 split(mnams, mnam, " "); split(numms, numm, " "); split(mday, mDays, " "); 14 for (i in mnam){mnum[mnam[i]] = numm[i]; mdays[numm[i]] = mDays[numm[i]];}; 15 oLday = 3 * 365 + 1 + mdays[mnum["Jan"]] + mdays[mnum["Feb"]]; 16 wnams = "sö mo ti on to fr lö"; split(wnams, wnam, " "); 17 # %End of DDay data. # ... 40 function wDay (DNUM) { 41 return wnam[1 + ((1 + DNUM) % 7)]; 42 }; 43 function DayDate (yY, mM, dD) { 44 dOnum = oLymp * int((yY - 1901) / 4); 45 dORnum = ((yY - 1901) % 4) * 365; 46 dYnum = 0; for (i = mM; i--; i > 0 ) dYnum += mdays[i]; 47 dYnum += dD; if ((mM > 2) && ((dYnum + dORnum) >= oLday)) dYnum++; 48 dNum = dYnum + dORnum + dOnum; 49 if ( \ 50 (mM < 13) && \ 51 ( (dD <= mdays[mM]) || \ 52 ((dYnum + dORnum) == oLday))) \ 53 return sprintf("%2s</td><td>%4d-%02d-%02d", wDay(dNum), yY, mM, dD); 54 };
Years: 0 Months: 1 Days: 3 Hours: 0 Minutes: 0 Seconds: 0
Years: 0 Months: 1 Days: 3 Hours: 0 Minutes: 0 Seconds: 0
// Class
function Period(startdate, enddate)
{
var d1 = startdate;
var d2 = enddate;
var secondsperminute = 60;
var minutesperhour = 60;
var hoursperday = 24;
var monthsperyear = 12;
var secondsperday = secondsperminute * minutesperhour * hoursperday;
var kilo = 1000;
var modneedsrefresh = 1;
var modyears = null;
var modmonths = null;
var moddays = null;
var modhours = null;
var modminutes = null;
var modseconds = null;
DoNewDates(d1, d2);
DiffWithBorrow(d1, d2);
function DoNewDates(d1, d2)
{
if (d2.getTime() < d1.getTime())
{
var tempd = new Date();
tempd.setTime(d2.getTime());
d2.setTime(d1.getTime());
d1.setTime(tempd.getTime());
}
modneedsrefresh = 1;
}
function DiffWithBorrow(d1, d2)
{
if (modneedsrefresh != 0)
{
modyears = 0;
modmonths = 0;
moddays = 0;
modhours = 0;
modminutes = 0;
modseconds = 0;
modseconds += d2.getUTCSeconds() - d1.getUTCSeconds();
if (modseconds < 0)
{
modseconds += secondsperminute;
modminutes -= 1;
}
modminutes += d2.getUTCMinutes() - d1.getUTCMinutes();
if (modminutes < 0)
{
modminutes += minutesperhour;
modhours -= 1;
}
modhours += d2.getUTCHours() - d1.getUTCHours();
if (modhours < 0)
{
modhours += hoursperday;
moddays -= 1;
}
moddays += d2.getUTCDate() - d1.getUTCDate();
if (moddays < 0)
{
var lastdayind1month = new Date(Date.UTC(d1.getUTCFullYear(), d1.getUTCMonth() + 1, 0));
moddays += lastdayind1month.getUTCDate();
modmonths -= 1;
}
modmonths += d2.getUTCMonth() - d1.getUTCMonth();
if (modmonths < 0)
{
modmonths += monthsperyear;
modyears -= 1;
}
modyears += d2.getUTCFullYear() - d1.getUTCFullYear();
modneedsrefresh = 0;
}
}
this.SetStartDate = function(startdate)
{
d1 = startdate;
DoNewDates(d1, d2);
}
this.SetEndDate = function(enddate)
{
d1 = enddate;
DoNewDates(d1, d2);
}
this.MilliSeconds = function()
{
return parseInt(d2 - d1);
}
this.Seconds = function()
{
return parseInt(this.MilliSeconds() / kilo);
}
this.Minutes = function()
{
return parseInt(this.MilliSeconds() / kilo * secondsperminute);
}
this.Hours = function()
{
return parseInt(this.MilliSeconds() / kilo * secondsperhour);
}
this.Days = function()
{
return parseInt(this.MilliSeconds() / (kilo * secondsperday));
}
this.Months = function()
{
DiffWithBorrow();
return modmonths + modyears * 12;
}
this.Years = function()
{
DiffWithBorrow();
return modyears;
}
this.ModularSeconds = function()
{
DiffWithBorrow();
return modseconds;
}
this.ModularMinutes = function()
{
DiffWithBorrow();
return modminutes;
}
this.ModularHours = function()
{
DiffWithBorrow();
return modhours;
}
this.ModularMonths = function()
{
DiffWithBorrow();
return modmonths;
}
this.ModularSummaryText = function()
{
DiffWithBorrow();
var result = '';
result += '' + modyears + ' years';
result += ', ' + modmonths + ' months';
result += ', ' + moddays + ' days';
result += ', ' + modhours + ' hours';
result += ', ' + modminutes + ' minutes';
result += ', ' + modseconds + ' seconds';
return result;
}
this.ModularCompareWith = function(cwyears, cwmonths, cwdays, cwhours, cwminutes, cwseconds)
{
var result = 0;
if (modyears < cwyears)
{
result = -1;
}
if (modyears > cwyears)
{
result = 1;
}
if (result == 0)
{
if (modmonths < cwmonths)
{
result = -1;
}
if (modmonths > cwmonths)
{
result = 1;
}
if (result == 0)
{
if (moddays < cwdays)
{
result = -1;
}
if (moddays > cwdays)
{
result = 1;
}
if (result == 0)
{
if (modhours < cwhours)
{
result = -1;
}
if (modhours > cwhours)
{
result = 1;
}
if (result == 0)
{
if (modminutes < cwminutes)
{
result = -1;
}
if (modminutes > cwminutes)
{
result = 1;
}
if (result == 0)
{
if (modseconds < cwseconds)
{
result = -1;
}
if (modseconds > cwseconds)
{
result = 1;
}
}
}
}
}
}
return result;
}
return this;
}
// Demo
function Demo()
{
var born = new Date(Date.UTC(1976, 7 -(1), 22, 20, 12, 00, 000));
var died = new Date(Date.now());
var life = new Period(born, died);
alert('Congratulation, you lived ' + life.Months() + ' months.');
alert(life.ModularSummaryText());
}
// Test cases
function Test()
{
var ok = 1;
var test;
var sd;
var ed;
var p;
var m = 1;
var d = 0;
if (ok)
{
test = "#1 (TS)";
sd = new Date(Date.UTC(2017, 3-m, 19-d, 00, 01, 01, 000));
ed = new Date(Date.UTC(2018, 1-m, 20-d, 00, 02, 01, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 10, 1, 0, 1, 0) == 0);
}
if (ok)
{
test = "#21a";
sd = new Date(Date.UTC(2018, 2-m, 3-d, 00, 00, 00, 000));
ed = new Date(Date.UTC(2018, 3-m, 3-d, 00, 00, 00, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 1, 0, 0, 0, 0) == 0);
}
if (ok)
{
test = "#21b";
sd = new Date(Date.UTC(2018, 2-m, 2-d, 00, 00, 00, 000));
ed = new Date(Date.UTC(2018, 3-m, 3-d, 00, 00, 00, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 1, 1, 0, 0, 0) == 0);
}
if (ok)
{
test = "#21c";
sd = new Date(Date.UTC(2017, 2-m, 2-d, 00, 00, 00, 000));
ed = new Date(Date.UTC(2018, 3-m, 3-d, 00, 00, 00, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(1, 1, 1, 0, 0, 0) == 0);
}
if (ok)
{
test = "#21d";
sd = new Date(Date.UTC(2000, 1-m, 1-d, 00, 00, 00, 000));
ed = new Date(Date.UTC(2018, 3-m, 3-d, 00, 00, 00, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(18, 2, 2, 0, 0, 0) == 0);
}
if (ok)
{
test = "The same time";
sd = new Date(Date.UTC(2013, 7-m, 5-d, 03, 19, 46, 000));
ed = new Date(Date.UTC(2013, 7-m, 5-d, 03, 19, 46, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 0, 0, 0, 0, 0) == 0);
}
if (ok)
{
test = "Exactly one month, aligned with new month";
sd = new Date(Date.UTC(2015, 8-m, 1-d, 00, 00, 00, 000));
ed = new Date(Date.UTC(2015, 9-m, 1-d, 00, 00, 00, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 1, 0, 0, 0, 0) == 0);
}
if (ok)
{
test = "Exactly one month, not aligned with new month";
sd = new Date(Date.UTC(2015, 8-m, 4-d, 16, 58, 06, 000));
ed = new Date(Date.UTC(2015, 9-m, 4-d, 16, 58, 06, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 1, 0, 0, 0, 0) == 0);
}
if (ok)
{
test = "Just short of reaching two months, misaligned begining"; // Miss in long month
sd = new Date(Date.UTC(2015, 8-m, 1-d, 00, 00, 01, 000));
ed = new Date(Date.UTC(2015, 10-m, 1-d, 00, 00, 00, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 1, 30, 23, 59, 59) == 0);
}
if (ok)
{
test = "Just short of reaching two months, misaligned ending"; // Miss in short month
sd = new Date(Date.UTC(2015, 8-m, 1-d, 00, 00, 00, 000));
ed = new Date(Date.UTC(2015, 9-m, 30-d, 23, 59, 59, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 1, 29, 23, 59, 59) == 0);
}
if (ok)
{
test = "Just overshooting two month";
sd = new Date(Date.UTC(2015, 1-m, 1-d, 00, 00, 00, 000));
ed = new Date(Date.UTC(2015, 3-m, 1-d, 00, 00, 01, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 2, 0, 0, 0, 1) == 0);
}
if (ok)
{
test = "Seven year";
sd = new Date(Date.UTC(2012, 5-m, 15-d, 14, 30, 00, 000));
ed = new Date(Date.UTC(2019, 5-m, 15-d, 14, 30, 00, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(7, 0, 0, 0, 0, 0) == 0);
}
if (ok)
{
test = "20-knuten";
sd = new Date(Date.UTC(1999, 12-m, 24-d, 12, 00, 00, 000));
ed = new Date(Date.UTC(2000, 1-m, 13-d, 12, 00, 00, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 0, 20, 0, 0, 0) == 0);
}
if (ok)
{
test = "Just over no leap day";
sd = new Date(Date.UTC(2017, 2-m, 26-d, 12, 00, 00, 000));
ed = new Date(Date.UTC(2017, 3-m, 2-d, 12, 00, 00, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 0, 4, 0, 0, 0) == 0);
}
if (ok)
{
test = "Just over a leap day";
sd = new Date(Date.UTC(2016, 2-m, 26-d, 12, 00, 00, 000));
ed = new Date(Date.UTC(2016, 3-m, 2-d, 12, 00, 00, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 0, 5, 0, 0, 0) == 0);
}
if (ok)
{
test = "100 days, begin in february, no leap year";
sd = new Date(Date.UTC(2017, 2-m, 26-d, 12, 00, 00, 000));
ed = new Date(Date.UTC(2017, 6-m, 6-d, 12, 00, 00, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 3, 8, 0, 0, 0) == 0);
}
if (ok)
{
test = "100 days, begin in february, leap year";
sd = new Date(Date.UTC(2016, 2-m, 26-d, 12, 00, 00, 000));
ed = new Date(Date.UTC(2016, 6-m, 6-d, 12, 00, 00, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 3, 9, 0, 0, 0) == 0);
}
if (ok)
{
test = "One hour short, beginning in long month";
sd = new Date(Date.UTC(2011, 3-m, 15-d, 18, 30, 00, 000));
ed = new Date(Date.UTC(2011, 4-m, 15-d, 17, 30, 00, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 0, 30, 23, 0, 0) == 0);
}
if (ok)
{
test = "One hour long, beginning in long month";
sd = new Date(Date.UTC(2011, 3-m, 15-d, 17, 30, 00, 000));
ed = new Date(Date.UTC(2011, 4-m, 15-d, 18, 30, 00, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 1, 0, 1, 0, 0) == 0);
}
if (ok)
{
test = "One hour short, beginning in short month";
sd = new Date(Date.UTC(2011, 4-m, 15-d, 18, 30, 00, 000));
ed = new Date(Date.UTC(2011, 5-m, 15-d, 17, 30, 00, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 0, 29, 23, 0, 0) == 0);
}
if (ok)
{
test = "One hour long, beginning in short month";
sd = new Date(Date.UTC(2011, 4-m, 15-d, 17, 30, 00, 000));
ed = new Date(Date.UTC(2011, 5-m, 15-d, 18, 30, 00, 000));
p = new Period(sd, ed);
ok = (p.ModularCompareWith(0, 1, 0, 1, 0, 0) == 0);
}
if (ok)
{
alert("Tests passed.");
}
else
{
var fail = '';
fail += 'Failed at test: ' + test + '\n';
fail += '\n';
fail += 'Start: ' + sd + '\n';
fail += 'End: ' + ed + '\n';
fail += 'Modular parts summary: ' + p.ModularSummaryText();
alert(fail);
}
}
// Class
function Period(startdate, enddate)
{
var d1 = startdate;
var d2 = enddate;
var secondsperminute = 60;
var minutesperhour = 60;
var hoursperday = 24;
var monthsperyear = 12;
var secondsperday = secondsperminute * minutesperhour * hoursperday;
var kilo = 1000;
var modneedsrefresh = 1;
var modyears = null;
var modmonths = null;
var moddays = null;
var modhours = null;
var modminutes = null;
var modseconds = null;
DoNewDates(d1, d2);
DiffWithBorrow(d1, d2);
function DoNewDates(d1, d2)
{
if (d2.getTime() < d1.getTime())
{
var tempd = new Date();
tempd.setTime(d2.getTime());
d2.setTime(d1.getTime());
d1.setTime(tempd.getTime());
}
modneedsrefresh = 1;
}
function DiffWithBorrow(d1, d2)
{
if (modneedsrefresh != 0)
{
modyears = 0;
modmonths = 0;
moddays = 0;
modhours = 0;
modminutes = 0;
modseconds = 0;
modseconds += d2.getUTCSeconds() - d1.getUTCSeconds();
if (modseconds < 0)
{
modseconds += secondsperminute;
modminutes -= 1;
}
modminutes += d2.getUTCMinutes() - d1.getUTCMinutes();
if (modminutes < 0)
{
modminutes += minutesperhour;
modhours -= 1;
}
modhours += d2.getUTCHours() - d1.getUTCHours();
if (modhours < 0)
{
modhours += hoursperday;
moddays -= 1;
}
moddays += d2.getUTCDate() - d1.getUTCDate();
if (moddays < 0)
{
var lastdayind1month = new Date(Date.UTC(d1.getUTCFullYear(), d1.getUTCMonth() + 1, 0));
moddays += lastdayind1month.getUTCDate();
modmonths -= 1;
}
modmonths += d2.getUTCMonth() - d1.getUTCMonth();
if (modmonths < 0)
{
modmonths += monthsperyear;
modyears -= 1;
}
modyears += d2.getUTCFullYear() - d1.getUTCFullYear();
modneedsrefresh = 0;
}
}
this.SetStartDate = function(startdate)
{
d1 = startdate;
DoNewDates(d1, d2);
}
this.SetEndDate = function(enddate)
{
d1 = enddate;
DoNewDates(d1, d2);
}
this.MilliSeconds = function()
{
return parseInt(d2 - d1);
}
this.Seconds = function()
{
return parseInt(this.MilliSeconds() / kilo);
}
this.Minutes = function()
{
return parseInt(this.MilliSeconds() / kilo * secondsperminute);
}
this.Hours = function()
{
return parseInt(this.MilliSeconds() / kilo * secondsperhour);
}
this.Days = function()
{
return parseInt(this.MilliSeconds() / (kilo * secondsperday));
}
this.Months = function()
{
DiffWithBorrow();
return modmonths + modyears * 12;
}
this.Years = function()
{
DiffWithBorrow();
return modyears;
}
this.ModularSeconds = function()
{
DiffWithBorrow();
return modseconds;
}
this.ModularMinutes = function()
{
DiffWithBorrow();
return modminutes;
}
this.ModularHours = function()
{
DiffWithBorrow();
return modhours;
}
this.ModularMonths = function()
{
DiffWithBorrow();
return modmonths;
}
this.ModularSummaryText = function()
{
DiffWithBorrow();
var result = '';
result += '' + modyears + ' years';
result += ', ' + modmonths + ' months';
result += ', ' + moddays + ' days';
result += ', ' + modhours + ' hours';
result += ', ' + modminutes + ' minutes';
result += ', ' + modseconds + ' seconds';
return result;
}
this.ModularCompareWith = function(cwyears, cwmonths, cwdays, cwhours, cwminutes, cwseconds)
{
var result = 0;
if (modyears < cwyears)
{
result = -1;
}
if (modyears > cwyears)
{
result = 1;
}
if (result == 0)
{
if (modmonths < cwmonths)
{
result = -1;
}
if (modmonths > cwmonths)
{
result = 1;
}
if (result == 0)
{
if (moddays < cwdays)
{
result = -1;
}
if (moddays > cwdays)
{
result = 1;
}
if (result == 0)
{
if (modhours < cwhours)
{
result = -1;
}
if (modhours > cwhours)
{
result = 1;
}
if (result == 0)
{
if (modminutes < cwminutes)
{
result = -1;
}
if (modminutes > cwminutes)
{
result = 1;
}
if (result == 0)
{
if (modseconds < cwseconds)
{
result = -1;
}
if (modseconds > cwseconds)
{
result = 1;
}
}
}
}
}
}
return result;
}
return this;
}
// Demo
function Demo()
{
var born = new Date(Date.UTC(1976, 7 -(1), 22, 20, 12, 00, 000));
var died = new Date(Date.now());
var life = new Period(born, died);
alert('Congratulation, you lived ' + life.Months() + ' months.');
alert(life.ModularSummaryText());
}
Flashback finansieras genom donationer från våra medlemmar och besökare. Det är med hjälp av dig vi kan fortsätta erbjuda en fri samhällsdebatt. Tack för ditt stöd!
Swish: 123 536 99 96 Bankgiro: 211-4106
Flashback finansieras genom donationer från våra medlemmar och besökare. Det är med hjälp av dig vi kan fortsätta erbjuda en fri samhällsdebatt. Tack för ditt stöd!
Swish: 123 536 99 96 Bankgiro: 211-4106