NovichokJS,
const getNewDate = initValue => {
const newValue = {
add(property, value) {
let set = `${property == 'year' ? 'setFull' : 'set'}`;
let get = `${property == 'year' ? 'getFull' : 'get'}`;
property = `${property.slice(0,1).toUpperCase()}${property.slice(1)}`;
set += property;
get += property;
value += initValue[get]();
initValue[set](value);
return this;
},
subtract(property, value) {
this.add(property, -value)
return this;
},
result() {
return initValue;
},
};
return newValue;
};
const res = getNewDate(new Date(2020, 0, 7, 17, 17, 17))
.add('minutes', 2)
.add('date', 8)
.subtract('year', 1)
.result();
// Output: ...Jan 15 2019 17:19:17.....
console.log(res);