New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rounding to the nearest 15 minute interval. #959
Comments
moment.fn.roundMinutes = function () { return ~(this.minutes() / 15) * 15; } |
I created this for my own use and figured it may help others. Keep in mind it does round to the nearest NEXT 15 min.
EDIT: removed this.incrementHours() in favor of this.add('hours', 1); |
Can you please add these functions to moment.. It would be really dang nice to be able to round to the nearest, next and previous time intervals... There are millions of google results for this and hundreds of stack overflow upvotes.. |
Now that I look at it again I have no idea how incrementHours was working. On Mon, Mar 10, 2014 at 2:29 PM, Blake Niemyjski
|
thanks for that method @zbarnett . Exactly what I needed for my project |
Well we can use the startOf interface like this |
Here's a pretty simple formula (in Coffeescript syntax, since that's the context where I ran into this issue):
I'm busy enough with other things right now, but if someone wants to take this code and put it straight into a PR, feel free. |
Closing in favor of #1595 |
No need to complicate, simple math:
Outputs to the closest 15 minute quarter. |
I couldn't get @janwerkhoven's solution to work, so here's mine:
|
@ground5hark function nearestMinutes(interval, someMoment){
const roundedMinutes = Math.round(someMoment.clone().minute() / interval) * interval;
return someMoment.clone().minute(roundedMinutes).second(0);
}
function nearestPastMinutes(interval, someMoment){
const roundedMinutes = Math.floor(someMoment.minute() / interval) * interval;
return someMoment.clone().minute(roundedMinutes).second(0);
}
function nearestFutureMinutes(interval, someMoment){
const roundedMinutes = Math.ceil(someMoment.minute() / interval) * interval;
return someMoment.clone().minute(roundedMinutes).second(0);
}
const now = moment();
const nearest5min = nearestMinutes(5, now);
const nearest15min = nearestMinutes(15, now);
const nearest30min = nearestMinutes(30, now);
const nearestFuture5min = nearestFutureMinutes(5, now);
const nearestPast5min = nearestPastMinutes(5, now); Tested on Code Pen The crucial bit is doing |
Try this guys time = moment("01:46", "HH:mm");
round_interval = 30;//15;
intervals = Math.floor(time.minutes() / round_interval);
minutesToRound = time.minutes() % round_interval;
minutesRounded = minutesToRound>round_interval/2 ? round_interval: 0;
minutes = intervals * round_interval + minutesRounded;
time.minutes(minutes);
alert(time.format("HH:mm")) |
Late to the party, I know, but... @janwerkhoven 's
@Silviusconcept 's solution overcomes this with |
@kevlarr |
Haha that's what I get for reading too many threads at the end of the day when I haven't slept.. I was doing |
I like zbarnett solution! Easyto use var newdate=turno.date.clone().roundNext15Min().add(anticipation, 'minutes'); moment.fn.roundNext15Min = function () { |
My version of an answer
Use like this:
These functions will change original object, use |
another solution would be: |
I was wondering if there is a way for me to round up or down to the nearest 15 minute interval. I think this would be great feature to have in moment as some libs like datejs kind of have this functionality from what I seen when searching. My scenario is that I have data points that are segmented into 15 minute buckets, The selection tool returns any block of time and I need to round down to the closest 15 minutes and round up for the end time.
Doing it this way (http://stackoverflow.com/questions/4968250/how-to-round-time-to-the-nearest-quarter-hour-in-javascript) seems like it would be a pain.. when It would be useful to have in the lib itself.
The text was updated successfully, but these errors were encountered: