Separate date and time

20-Sep-2017 13:49:56 (cell array) How can i separate date and time in two separate variables. 20-Sep-2017 in one variable and 13:49:56 in other variable?

回答 (3 件)

Andrei Bobrov
Andrei Bobrov 2017 年 10 月 24 日

1 投票

a - your cell array
t = datetime(a);
v = datevec(t);
Var1 = datetime(v(:,1:3));
Var2 = duration(v(:,4:end));

2 件のコメント

Steph
Steph 2019 年 2 月 11 日
this seperates a datetetime object into a datetime and duration object.
I couldnt merge these different datetime character vectors with a double.
Peter Perkins
Peter Perkins 2019 年 2 月 12 日
With no context for this comment, it's hard to provide any help.
datetiome and duration arrays display in a human-readable format, but they are not "character vectors". You almost certainly do not want to use text to store your timestamps, datetimes and durations are a much better choice.
If by "couldn't merge", you mean "create one array that contains both timestamps and other numeric data", then use a table. If you mean, "add a datetime and a numeric value", you can do that, and it will be equivalent to adding in units of days, but I would recommend against it. Use datetimes and durations, and save yourself the headache of needing to rembering what units your numbers happen to be in.
Don't mix datetime/duration with datenum/datestr. In fact, unless you have a good reason, or a version of MATLAB prior to R2014b, don't use datenum/datestr at all.

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

KL
KL 2017 年 10 月 24 日

0 投票

if you have everything in datetime format inside the cell array, use this,
dt = [C{:}'];
datesonly = [dt.Day dt.Month dt.Year]
timesOnly = [dt.Hour dt.Minute dt.Second]
Peter Perkins
Peter Perkins 2017 年 11 月 16 日

0 投票

>> dt = datetime
dt =
datetime
15-Nov-2017 23:54:20
>> t = timeofday(dt)
t =
duration
23:54:20
>> d = dt - t
d =
datetime
15-Nov-2017 00:00:00

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

タグ

質問済み:

2017 年 10 月 24 日

コメント済み:

2019 年 2 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by