Separate date and time

17 views (last 30 days)
sikander shahzad
sikander shahzad on 24 Oct 2017
Commented: Peter Perkins on 12 Feb 2019
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?

Answers (3)

Andrei Bobrov
Andrei Bobrov on 24 Oct 2017
a - your cell array
t = datetime(a);
v = datevec(t);
Var1 = datetime(v(:,1:3));
Var2 = duration(v(:,4:end));
  2 Comments
Steph
Steph on 11 Feb 2019
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 on 12 Feb 2019
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.

Sign in to comment.


KL
KL on 24 Oct 2017
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 on 16 Nov 2017
>> 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

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!