フィルターのクリア

changing minutes after midnight into a datetime array

4 ビュー (過去 30 日間)
AA
AA 2017 年 10 月 26 日
コメント済み: Walter Roberson 2017 年 11 月 20 日
Suppose you have the matrix minutesaftermid which contains integers that represent the minutes after midnight, e.g. 1,2,3,4,5,6,7,......
How do I convert this format to a datetime array.
t = datetime(Y,M,D,H,MI,S,MS), but I only needs the MI.

採用された回答

per isakson
per isakson 2017 年 10 月 26 日
編集済み: per isakson 2017 年 10 月 26 日
"but I only need the MI" I don't think you can avoid the date and time.
X = 60*[1,2,3,4,5,6,7];
T = datetime( X, 'ConvertFrom','epochtime', 'Epoch','2000-01-01' )
T =
Columns 1 through 4
2000-01-01 00:01:00 2000-01-01 00:02:00 2000-01-01 00:03:00 2000-01-01 00:04:00
Columns 5 through 7
2000-01-01 00:05:00 2000-01-01 00:06:00 2000-01-01 00:07:00
and
>> T.Minute
ans =
1 2 3 4 5 6 7
Remark: the year 2000 is a leap year.

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2017 年 10 月 26 日
datetime('today') + minutes(minutesaftermid)
if you wanted it to be relative to today (local time)
If you just need minutes after midnight and not any particular day, you should probably be leaving it as a duration array instead of a datetime array.

Peter Perkins
Peter Perkins 2017 年 11 月 16 日
datetimes are dates+times. As Walter says, unless you also have a set of dates, or you want 'today', then you likely want a duration array, and so just call the minutes function to create one.
  3 件のコメント
Peter Perkins
Peter Perkins 2017 年 11 月 20 日
Ah, right. So you can either add minutes (as you showed), or if you have the date portion as numeric values, you can use the "roll-over" behavior built into the constructor:
>> datetime(2017,11,1:3,0,[1430 1440 1450],0)
ans =
1×3 datetime array
01-Nov-2017 23:50:00 03-Nov-2017 00:00:00 04-Nov-2017 00:10:00
Walter Roberson
Walter Roberson 2017 年 11 月 20 日
I might...
T = datetime(2017,11,1:3,0,[1430 1440 1450],0, 'TimeZone', 'local');
[H, M] = hms(T);
mins_since_midnight = H * 60 + M
... though I would want to double-check outputs when there are time zone changes. I have now gotten myself confused about what "minutes since midnight" even means when there are time-zone changes.

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

カテゴリ

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