Skip to content

Instantly share code, notes, and snippets.

@ilomon10
Created March 2, 2021 13:04
Show Gist options
  • Save ilomon10/12aba71af32cf4c0992a912e1c883f55 to your computer and use it in GitHub Desktop.
Save ilomon10/12aba71af32cf4c0992a912e1c883f55 to your computer and use it in GitHub Desktop.
Round moment.js object time to nearest interval
// Ref: https://stackoverflow.com/a/25323966/8271526
function dateRounding(interval, unit, date) {
const start = moment(date);
const remainder = interval - (start.get(unit) % interval);
const dateTime = moment(start).add(remainder, unit).format("DD.MM.YYYY, h:mm:ss a");
return dateTime;
}
dateRounding(30, "minute", "2018-12-08 09:42");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment