Separate date and time
17 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(3 个)
Andrei Bobrov
2017-10-24
a - your cell array
t = datetime(a);
v = datevec(t);
Var1 = datetime(v(:,1:3));
Var2 = duration(v(:,4:end));
2 个评论
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
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
2017-10-24
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]
0 个评论
Peter Perkins
2017-11-16
>> 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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!