Round minute down to nearest quarter hour
Your full function would be something like this...
function roundToQuarterHour($timestring) {
$minutes = date('i', strtotime($timestring));
return $minutes - ($minutes % 15);
}
How to round time to the nearest quarter hour in java?
Rounding
You will need to use modulo to truncate the quarter hour:
Date whateverDateYouWant = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(whateverDateYouWant);
int unroundedMinutes = calendar.get(Calendar.MINUTE);
int mod = unroundedMinutes % 15;
calendar.add(Calendar.MINUTE, mod < 8 ? -mod : (15-mod));
As pointed out by EJP, this is also OK (replacement for the last line, only valid if the calendar is lenient):
calendar.set(Calendar.MINUTE, unroundedMinutes + mod);
Improvements
If you want to be exact, you will also have to truncate the smaller fields:
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
You can also use DateUtils.truncate() from Apache Commons / Lang to do this:
calendar = DateUtils.truncate(calendar, Calendar.MINUTE);
Rounding time to nearest quarter hour
Indeed, an old question with some helpful answers so far. The last one by giraffhere seems to be the most elegant. however, ist not floor_date but round_date which will do the trick:
lubridate::round_date(x, "15 minutes")
make function round up to nearest quarter hour
How about this:
new Date(Math.ceil(new Date().getTime()/900000)*900000);
Explanation: new Date().getTime()
returns the current time in the form of a unix timestamp (i.e. the number of milliseconds since 1970-01-01 00:00 UTC), which we round up to the nearest multiple of 900000 (i.e. the number of milliseconds in a quarter-hour) with the help of Math.ceil
.
Edit: If you want to apply this to intervals other than 15 minutes, you can do it like that (e.g. for 30 minutes):
var interval = 30 * 60 * 1000; // 30 minutes in milliseconds
new Date(Math.ceil(new Date().getTime()/interval)*interval);
How do I round datetime column to nearest quarter hour
Assuming that your series is made up of datetime
objects, You need to use Series.apply
. Example -
import datetime
df['<column>'] = df['<column>'].apply(lambda dt: datetime.datetime(dt.year, dt.month, dt.day, dt.hour,15*(dt.minute // 15)))
The above example to always round to the previous quarter hour (behavior similar to floor function).
EDIT
To round to the correct quarter hour (as in , if its 7 mins 30 seconds past previous quarter, to show the next quarter) . We can use the below example -
import datetime
df['<column>'] = df['<column>'].apply(lambda dt: datetime.datetime(dt.year, dt.month, dt.day, dt.hour,15*round((float(dt.minute) + float(dt.second)/60) / 15)))
The above would only take the latest seconds into consideration , if you want the millisecond/microsecond into consideration , you can add that to the above equation as - (float(dt.minute) + float(dt.second)/60 + float(dt.microsecond)/60000000)
How to round time to the nearest quarter hour in Dart and Flutter?
You can do this in Dart and Flutter with an Extension on DateTime:
extension on DateTime{
DateTime roundDown({Duration delta = const Duration(seconds: 15)}){
return DateTime.fromMillisecondsSinceEpoch(
this.millisecondsSinceEpoch -
this.millisecondsSinceEpoch % delta.inMilliseconds
);
}
}
Usage:
DateTime roundedDateTime = DateTime.now().roundDown();
print(roundedDateTime);
Output: "2020-03-16 12:23:45.000"
or
DateTime roundedDateTime = DateTime.now().roundDown(delta: Duration(hour: 1));
print(roundedDateTime);
Output: "2020-03-16 12:00:00.000"
c# - rounding time values down to the nearest quarter hour
x = x - (x % 15)
would be a solution that doesn't rely on integer division.
How to round time to the nearest quarter hour in JavaScript?
Given that you have hours and minutes in variables (if you don't you can get them from the Date
instance anyway by using Date
instance functions):
var m = (parseInt((minutes + 7.5)/15) * 15) % 60;
var h = minutes > 52 ? (hours === 23 ? 0 : ++hours) : hours;
minutes can as well be calculated by using Math.round()
:
var m = (Math.round(minutes/15) * 15) % 60;
or doing it in a more javascript-sophisticated expression without any functions:
var m = (((minutes + 7.5)/15 | 0) * 15) % 60;
var h = ((((minutes/105) + .5) | 0) + hours) % 24;
You can check the jsPerf test that shows Math.round()
is the slowest of the three while mainly the last one being the fastest as it's just an expression without any function calls (no function call overhead i.e. stack manipulation, although native functions may be treated differently in Javascript VM).
//----
Related Topics
Create Barplot from Data.Frame
Increase Space Between Bars in Ggplot
Linear Model Function Lm() Error: Na/Nan/Inf in Foreign Function Call (Arg 1)
How to Change Angle of Line in Customized Legend in Ggplot2
Adding 15 Business Days in Lubridate
Regression (Logistic) in R: Finding X Value (Predictor) for a Particular Y Value (Outcome)
How to Split an Igraph into Connected Subgraphs
How to Update a Shiny Fileinput Object
Generate Ggplot2 Boxplot with Different Colours for Multiple Groups
Generate All Possible Permutations (Or N-Tuples)