Created
July 11, 2015 18:06
-
-
Save ivan-loh/41215fa342189dbc340e to your computer and use it in GitHub Desktop.
javascript - get date to closest x minutes
This file contains hidden or 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 getRange() { | |
| var lower = getLowerClosest(10); | |
| var upper = getHigherClosest(10); | |
| return moment(lower).format('HH:mm:ss') + ' - ' + moment(upper).format('HH:mm:ss'); | |
| } | |
| function getLowerClosest(mins) { | |
| var coeff = 1000 * 60 * mins; | |
| var date = new Date(); | |
| return new Date(Math.floor(date.getTime() / coeff) * coeff); | |
| } | |
| function getHigherClosest(mins) { | |
| var coeff = 1000 * 60 * mins; | |
| var date = new Date(); | |
| return new Date(Math.ceil(date.getTime() / coeff) * coeff); | |
| } | |
| function getClosest(mins) { | |
| var coeff = 1000 * 60 * mins; | |
| var date = new Date(); | |
| return new Date(Math.round(date.getTime() / coeff) * coeff); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment