How to convert datetime vector to military time

15 ビュー (過去 30 日間)
Asher Zaidi
Asher Zaidi 2018 年 6 月 18 日
コメント済み: Stephen23 2018 年 7 月 5 日
This is my code:
files = dir('*.xlsx');
for i=1:10
[~, ~, raw] = xlsread(files(i).name);
rawm = raw(5:6,1);
rawmd = cell2mat(rawm(1));
rawmt = cell2mat(rawm(2));
s = strcat(rawmd(31:40),rawmt(30:38));
ds(i) = datetime(s)
end
It produces a vector that looks like this:
ds'
ans =
10×1 datetime array
14-Feb-2018 11:34:16
15-Feb-2018 12:34:58
15-Feb-2018 01:35:04
15-Feb-2018 02:35:26
15-Feb-2018 03:35:52
15-Feb-2018 04:35:59
15-Feb-2018 05:36:05
15-Feb-2018 06:36:14
15-Feb-2018 07:36:41
15-Feb-2018 08:36:48
How can I change the format of the datetime array to military time so that the times may appear in chronological order?

採用された回答

Stephen23
Stephen23 2018 年 6 月 18 日
編集済み: Stephen23 2018 年 7 月 5 日
"How can I change the format of the datetime array to military time so that the times may appear in chronological order?"
You don't need to: the Format of datetime objects relates to how the datetime is displayed, and is unrelated to how it is stored internally, so changing how it looks does not change how it will be sorted. If you want chronological order just apply sort and see what happens:
sort(ds)
If you want to change how the datetime is displayed then you can change its Format property. The ISO 8601 date standard is the best to use, as is guarantees chronological order when sorted by character:
T = datetime(...);
T.Format = 'yyyyMMddHHmmss';
  3 件のコメント
Peter Perkins
Peter Perkins 2018 年 7 月 5 日
Stephen, I think maybe you meant (H vs. h)
T.Format = 'yyyyMMddHHmmss';
Stephen23
Stephen23 2018 年 7 月 5 日
@Peter Perkins: thank you, I fixed my answer.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by