Skip to content

Instantly share code, notes, and snippets.

@jkeefe
Created August 3, 2023 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkeefe/2df36cde9ba67dd13a6954129b075143 to your computer and use it in GitHub Desktop.
Save jkeefe/2df36cde9ba67dd13a6954129b075143 to your computer and use it in GitHub Desktop.
Sweet dayjs rounding function
// from https://github.com/iamkun/dayjs/issues/1619#issuecomment-1185714859
const round: PluginFunc = (option, dayjsClass) => {
dayjsClass.prototype.round = function (amount, unit) {
const mod = this.get(unit as UnitType) % amount;
if(mod < amount / 2) {
return this.subtract(mod, unit).startOf(unit);
}
return this.add(amount - mod, unit).startOf(unit);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment