Borrar filtros
Borrar filtros

how to combine date and time in single column

9 visualizaciones (últimos 30 días)
MONICA RAWAT
MONICA RAWAT el 22 de Mayo de 2020
Comentada: Steven Lord el 19 de Abr. de 2023
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 comentarios
KSSV
KSSV el 22 de Mayo de 2020
Editada: KSSV el 22 de Mayo de 2020
strjoin, strcat.
[date time]
MONICA RAWAT
MONICA RAWAT el 22 de Mayo de 2020
both of these function is not working

Iniciar sesión para comentar.

Respuestas (1)

Steven Lord
Steven Lord el 22 de Mayo de 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 comentarios
Walter Roberson
Walter Roberson el 19 de Abr. de 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 el 19 de Abr. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by