Skip to content

Instantly share code, notes, and snippets.

@Ivannnnn
Last active September 24, 2022 13:36
Show Gist options
  • Save Ivannnnn/4cfb13b71874db522f558aa4765d3efc to your computer and use it in GitHub Desktop.
Save Ivannnnn/4cfb13b71874db522f558aa4765d3efc to your computer and use it in GitHub Desktop.
function date(date = new Date(), props = {}) {
[date, props] =
date.constructor.name === "Object"
? [new Date(), date]
: [new Date(date), props];
for (let key in props) {
if (typeof props[key] === "string" && props[key].match(/^[+-]/))
props[key] = date[key.replace(/^set/, "get")]() + parseInt(props[key]);
date[key](props[key]);
}
return date;
}
/* USAGE:
date('2022-09-18')
date(new Date('2022-09-17'))
date('2022-09-19', { setDate: '-1' })
date('2022-09-19', { setDate: 25 })
date({ setDate: '-1' })
date()
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment