how to combine date and time in single column

17 views (last 30 days)
suppose i have one table of date and another table of time as given below
date time
2013-11-23 23:32:29
2013-12-24 22:29:38
then i want my output to be given below in a single table
datetime
2013-11-23 23:32:29
2013-12-24 22:29:38
  2 Comments
KSSV
KSSV on 22 May 2020
Edited: KSSV on 22 May 2020
strjoin, strcat.
[date time]
MONICA RAWAT
MONICA RAWAT on 22 May 2020
both of these function is not working

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 22 May 2020
In what data type is your data stored?
If it's stored as char vectors then you can use the functions KSSV mentioned in the comment.
C = table();
C.date = ['2013-11-23'; '2013-12-24'];
C.time = ['23:32:29'; '22:29:38'];
C.dateAndTime = strcat(C.date, {' '}, C.time)
If they are a datetime and a duration just add them together.
C = table();
C.date = datetime({'2013-11-23'; '2013-12-24'});
C.time = duration({'23:32:29'; '22:29:38'});
C.dateAndTime = C.date + C.time
  4 Comments
Walter Roberson
Walter Roberson on 19 Apr 2023
Suppose you have an array TimeOfDay that is intended to be hour/minutes/seconds but got stored in the form of datetime, and suppose you have a Date array that got stored in the form of datetime. Then
DateTime = Date + (TimeOfDay - dateshift(TimeOfDay, 'start', 'day'));
As usual you should be asking questions about what happens when Daylight Savings Time begins or ends.
Steven Lord
Steven Lord on 19 Apr 2023
I have both dates and times as datetime and when I try adding them I get error that says addition is not possible between datetime arrays.
That's correct. What would you expect the result to be if you could add today and tomorrow together? No, some date and time in the year 4046 would not be the correct answer IMO.
The dateshift approach Walter suggested is one possible solution, another would be to use the hms function on one of the datetime arrays.
dt = datetime('now')
dt = datetime
19-Apr-2023 23:25:18
[h1, m1, s1] = hms(dt)
h1 = 23
m1 = 25
s1 = 18.0110
du = duration(h1, m1, s1)
du = duration
23:25:18

Sign in to comment.

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!