Round date and hour to the previous 15 minute interval

39 ビュー (過去 30 日間)
Jose Valles
Jose Valles 2018 年 3 月 8 日
コメント済み: Jim McIntyre 2020 年 4 月 28 日
Hello
I would like to round the datetime variable to the previous 15 minutes interval. The tricky part is that it has to be based on the current time. For instance:
t1 = datetime('now','Format','yyyy-MM-dd HH:mm')
the result of the previous code is
2018-03-08 10:34
So, I would like to obtain ...
2018-03-08 10:30
I have tried to used the dateshift function however, I haven't had a good solution

採用された回答

Rik
Rik 2018 年 3 月 8 日
編集済み: Rik 2018 年 3 月 8 日
Dateshift would be a good solution, but it can only shift to quarter years, not quarter hours. The code below is a way around this.
t1 = datetime('now','Format','yyyy-MM-dd HH:mm')
t1.Minute = 15 * floor(t1.Minute/15)
%original code:
% t1 = datetime('now','Format','yyyy-MM-dd HH:mm');
% %syntax: t = datetime(Y,M,D,H,MI,S);
% t1=datetime(year(t1),month(t1),day(t1),hour(t1),15*floor(minute(t1)/15),0);
  4 件のコメント
Rik
Rik 2018 年 3 月 8 日
Good point, I'll edit my answer.
Jim McIntyre
Jim McIntyre 2020 年 4 月 28 日
This answer works for the example given, but is incomplete for real times that might also have non-zero values for seconds. The answer rounds the minutes, but leaves the seconds unrounded.
To fully round to minutes, you need to add:
t1.Second = 0

サインインしてコメントする。

その他の回答 (2 件)

Steven Lord
Steven Lord 2018 年 3 月 8 日
Let's start off with the current date and time.
N = datetime('now')
Figure out the start of the hour containing N.
H = dateshift(N, 'start', 'hour')
Generate a datetime vector spaced a quarter of an hour apart, starting with H.
Q = hours(0.25)
P = H + Q*(0:4)
If you're using release R2016b or later you can use discretize.
previousQuarterHour = P(discretize(N, P))
If you're using release R2015b or later you can interpolate using interp1.
previousQuarterHour = interp1(P, P, N, 'previous')
If you're using release R2014b or R2015a you'll need to use find to find the last element of P that is less than or equal to N.
previousQuarterHour = P(find(P <= N, 1, 'last'))
  1 件のコメント
Jose Valles
Jose Valles 2018 年 3 月 8 日
Thanks!! it is also a good result

サインインしてコメントする。


Jose Valles
Jose Valles 2018 年 3 月 8 日
編集済み: Jose Valles 2018 年 3 月 8 日
Thank for your reply!!
One quick question always related to this post: if I want to find the previous 15 minutes, what would be the best way to do it?
For example, for a datetime variable like:
2018-03-08 08:06
2018-03-08 10:34
we want the last 15-minute as shown as
2018-03-08 07:15
2018-03-08 10:15
What would be the efficient way
Thank you so much!
  3 件のコメント
Jose Valles
Jose Valles 2018 年 3 月 8 日
No. What I want is to round to previous 15 minutes (Not the previous 15 minutes interval). For instance, the datetime is '2018-03-08 08:06' so the result that I am looking for is '2018-03-08 07:15'.
Another example: the datetime is '2018-03-08 10:34' so the result that i am looking is '2018-03-08 10:15'
I just realized that my example from the previous post was innacurated. My apologies for the innacurate example
Rik
Rik 2018 年 3 月 8 日
t1 = datetime('now','Format','yyyy-MM-dd HH:mm')
t1.Minute = 60 * floor((t1.Minute-15)/60)+15
This will round to the previous ##:15

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by