Round down to a chosen hour-min-sec time at a date
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
I use
now_time = datetime(now,'ConvertFrom','datenum')
Assume it gives me
ans =
datetime
14-May-2019 02:28:24
Then assume I have some chosen time settings hh:mm:ss, eg:
A = [ 02:00:00 18:00:00 08:00:00 ]
I would like something to go back to the nearest time from my settings, in this case it should give me this (due to above now_time):
14-May-2019 02:00:00
If the now_time was 14-May-2019 01:59:59, then due to its before 02:00:00, I would like it to go to the time backwards "round down":
13-May-2019 18:00:00
Another example: Or if now_time is 13-May-2019 08:00:01
I would like:
13-May-2019 08:00:00
Hope I am clear enourgh and hope for some advise. Thanks in advance
0 Kommentare
Antworten (2)
Walter Roberson
am 14 Mai 2019
B = dateshift(now_time, 'start', 'day') + A_in_duration_form;
interp1(B, B, now_time, 'prev')
5 Kommentare
Walter Roberson
am 14 Mai 2019
A = [ 02:00:00 18:00:00 08:00:00 ]
Those values need to be in sorted order, especially when you talk about going back days.
"from 2 in the morning until 6pm and then until 8am the next day but go back a day if you need to" is ambiguous: is 03:00 in the 02:00:00 to 18:00:00 range, or is it in the 18:00:00 to 08:00:00 range?
If you want to start from a particular day (for example you might have found midnight Sunday) and proceed on a multiday schedule, then use appropriate offsets, such as [hours(2) hours(18) hours(24+8)
Peter Perkins
am 4 Jun. 2019
Since you're working in liear time (preserving dates) and no in circular time (caring only about time of day), I would think discretize would be a good way to do this:
>> dt = datetime('today') + hours(sort(48*rand(10,1)))
dt =
10×1 datetime array
04-Jun-2019 01:48:41
04-Jun-2019 04:44:17
04-Jun-2019 12:34:11
04-Jun-2019 17:38:13
04-Jun-2019 18:59:05
05-Jun-2019 08:35:54
05-Jun-2019 14:13:00
05-Jun-2019 18:29:17
05-Jun-2019 19:50:15
05-Jun-2019 23:25:23
>> bins = datetime(2019,6,[4 4 4 4 5 5 5 5],[0 2 8 18 2 8 18 24],0,0)
bins =
1×8 datetime array
Columns 1 through 5
04-Jun-2019 00:00:00 04-Jun-2019 02:00:00 04-Jun-2019 08:00:00 04-Jun-2019 18:00:00 05-Jun-2019 02:00:00
Columns 6 through 8
05-Jun-2019 08:00:00 05-Jun-2019 18:00:00 06-Jun-2019 00:00:00
>> discretize(dt,bins,bins(1:end-1))
ans =
10×1 datetime array
04-Jun-2019 00:00:00
04-Jun-2019 02:00:00
04-Jun-2019 08:00:00
04-Jun-2019 08:00:00
04-Jun-2019 18:00:00
05-Jun-2019 08:00:00
05-Jun-2019 08:00:00
05-Jun-2019 18:00:00
05-Jun-2019 18:00:00
05-Jun-2019 18:00:00
0 Kommentare
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!