class CDate extends Date {
constructor (...args) {
super (...args)
}
add (what) {
if (what.seconds) {
this.setTime(this.valueOf() + what.seconds*1000);
}
if (what.minutes) {
this.setTime(this.valueOf() + what.minutes*60*1000);
}
if (what.hours) {
this.setTime(this.valueOf() + what.hours*60*60*1000);
}
if (what.days) {
this.setTime(this.valueOf() + what.days*24*60*60*1000);
}
if (what.weeks) {
this.setTime(this.valueOf() + what.weeks*7*24*60*60*1000);
}
if (what.months) {
let curm = this.getMonth();
let cury = this.getFullYear();
cury += what.months / 12 | 0;
curm += what.months % 12;
if (curm >=12) {
curm -= 12;
cury ++;
}
const nd = new Date (cury, curm,
this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds());
this.setTime(nd.valueOf());
}
if (what.years) {
let cury = this.getFullYear() + what.years;
const nd = new Date (cury, this.getMonth(),
this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds());
this.setTime(nd.valueOf());
}
}
sub (what) {
this.add ({
years: - (what.years||0),
months: - (what.months||0),
weeks: - (what.weeks||0),
days: - (what.days||0),
hours: - (what.hours||0),
minutes: - (what.minutes||0),
seconds: - (what.seconds||0),
}
)
}
}
dt = new CDate();
console.log (dt)
dt.add({months:15, weeks:2, days:4, hours: 10, minutes:50})
console.log ('add', dt)
dt1 = new CDate();
console.log (dt1)
dt1.sub({years: 3, months:10, weeks:2, minutes:18})
console.log ('sub', dt1)