I would like to merge two different column in one as datetime

1 view (last 30 days)
I want to merge these two column in one column as datetime column
  3 Comments
Younes
Younes on 29 Aug 2023
Thanks stephen
I am not using month and year the type of the datetime data that i want like "DD HH:mm"
Stephen23
Stephen23 on 29 Aug 2023
"I am not using month and year the type of the datetime data that i want like "DD HH:mm""
DATETIME objects always have a year and month.
It sould like you should be using a DURATION object instead:

Sign in to comment.

Answers (2)

Katy Weihrich
Katy Weihrich on 29 Aug 2023
I added two versions. I think you will wanted Version 2 but Version 1 illustrates how to use durations. I prefere to work with durations when no date is given (see Version 1).
%% simulate data
Date = table(ones(9,1),'VariableNames',{'DAY'});
Time = table(char("00:"+string(num2str((0:5:40)','%02.f'))),'VariableNames',{'TIME'});
%% Version 1) transform the number varibale of "Day" into a duration variable
% transform the numeric data into duration
iday = days(Date.DAY);
% transform the string data into datetime
itime = datetime(string(Time.TIME),"InputFormat","hh:mm");
% transform the datetime data into duration
imin = minutes(minute(itime));
ih = hours(hour(itime));
% add day + h + min durations together
iduration = iday+ih+imin;
% Output:
% iduration =
% 9×1 duration array
% 1 day
% 1.0035 days
% 1.0069 days
% 1.0104 days
% 1.0139 days
% 1.0174 days
% 1.0208 days
% 1.0243 days
% 1.0278 days
%% Version 2: transform the char data into datetime, remove today and add the day as duration
% note: if no day is given, datetime gives you the current day and time
% >> datetime
% ans =
% datetime
% 29-Aug-2023 15:29:24
% >> datetime(string(Time.TIME(5,:)),"InputFormat","hh:mm")
% ans =
% datetime
% 29-Aug-2023 00:20:00
itime = datetime(string(Time.TIME),"InputFormat","hh:mm") - today('datetime') + days(Date.DAY);
% Output:
% itime =
% 9×1 duration array
% 24:00:00
% 24:05:00
% 24:10:00
% 24:15:00
% 24:20:00
% 24:25:00
% 24:30:00
% 24:35:00
% 24:40:00
  4 Comments
Katy Weihrich
Katy Weihrich on 31 Aug 2023
Thank you. I keep forgetting that you can change datetimes properties this way.

Sign in to comment.


Stephen23
Stephen23 on 29 Aug 2023
% Fake data:
A = ones(5,1)
A = 5×1
1 1 1 1 1
B = {'0:00';'0:05';'0:10';'0:15';'0:20'}
B = 5×1 cell array
{'0:00'} {'0:05'} {'0:10'} {'0:15'} {'0:20'}
% Convert to DURATION:
M = str2double(split(B,':'));
D = days(A)+duration(M(:,1),M(:,2),0);
D.Format = 'dd:hh:mm:ss'
D = 5×1 duration array
01:00:00:00 01:00:05:00 01:00:10:00 01:00:15:00 01:00:20:00

Categories

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

Community Treasure Hunt

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

Start Hunting!