Separate date and time

조회 수: 17 (최근 30일)
sikander shahzad
sikander shahzad 2017년 10월 24일
댓글: Peter Perkins 2019년 2월 12일
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일
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일
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일
>> 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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by