Skip to content

Instantly share code, notes, and snippets.

@clichedmoog
Created December 15, 2017 18:05
Show Gist options
  • Save clichedmoog/eada84db5f0bafbf0a13443afe416d0b to your computer and use it in GitHub Desktop.
Save clichedmoog/eada84db5f0bafbf0a13443afe416d0b to your computer and use it in GitHub Desktop.
add clone, add for javascript Date object
Date.prototype.clone = function () {
return new Date(this.getTime());
};
Date.prototype.add = function (val, unit) {
d = new Date(this.getTime());
switch (unit) {
case 'year':
case 'years':
d.setYear(d.getYear() + val);
break;
case 'month':
case 'months':
d.setMonth(d.getMonth() + val);
break;
case 'day':
case 'days':
d.setDate(d.getDate() + val);
break;
case 'hour':
case 'hours':
d.setHours(d.getHours() + val);
break;
case 'minute':
case 'minutes':
d.getMinutes(d.getMinutes() + val);
break;
case 'second':
case 'seconds':
d.getSeconds(d.getSeconds() + val);
break;
default:
throw 'Invalid Unit; should be years, month, days, hours, minutes, seconds';
}
return d;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment