Skip to content

Instantly share code, notes, and snippets.

@269652
Created March 4, 2020 15:30
Show Gist options
  • Save 269652/262c544d4f4e526df93634586040d60b to your computer and use it in GitHub Desktop.
Save 269652/262c544d4f4e526df93634586040d60b to your computer and use it in GitHub Desktop.
Round to the nearest time interval using moment.js
function roundDown (ts, duration) {
const now = moment(ts);
const dur = duration.split (" ");
dur [0] = +dur[0];
const rest = (now[dur[1]]() % dur[0])
const start = now.clone().subtract(rest, dur[1]).startOf(dur[1])
return +start;
}
roundDown(+new Date, '15 minutes')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment