how to combine date and time in single column

9 次查看(过去 30 天)
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

回答(1 个)

Steven Lord
Steven Lord 2020-5-22
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 个评论
Walter Roberson
Walter Roberson 2023-4-19
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 2023-4-19
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

请先登录,再进行评论。

类别

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