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

I want to merge these two column in one column as datetime column

3 Comments

For what year and month? Is "DAY" to be interpreted as the day of the month, or day of the year, or day since some epoch?
Thanks stephen
I am not using month and year the type of the datetime data that i want like "DD HH:mm"
"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)

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

if you want it as datetime formated like this: "DD HH:mm" use:
itime = datetime(string(Time.TIME),"InputFormat","hh:mm") - today('datetime') + days(Date.DAY);
itime = datetime(datenum(itime),'ConvertFrom','datenum','Format',"dd HH:mm")
"if you want it as datetime formated like this: "DD HH:mm" use: "
No, do NOT convert to serial date number with its assocated loss of precision. Avoid deprecated DATENUM etc.
Simply adjust the FORMAT of the DATETIME object e.g.:
DT= datetime('now')
DT = datetime
31-Aug-2023 04:32:21
DT.Format = 'dd HH:mm' % simpler, no loss of precision, more efficient
DT = datetime
31 04:32
Thank you. I keep forgetting that you can change datetimes properties this way.
Thank you very much for your help

Sign in to comment.

% 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

Asked:

on 29 Aug 2023

Commented:

on 1 Sep 2023

Community Treasure Hunt

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

Start Hunting!