Created
March 4, 2020 15:30
-
-
Save C5H8NNaO4/262c544d4f4e526df93634586040d60b to your computer and use it in GitHub Desktop.
Round to the nearest time interval using moment.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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