Effacer les filtres
Effacer les filtres

how to combine date and time in single column

9 vues (au cours des 30 derniers jours)
MONICA RAWAT
MONICA RAWAT le 22 Mai 2020
Commenté : Steven Lord le 19 Avr 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 commentaires
KSSV
KSSV le 22 Mai 2020
Modifié(e) : KSSV le 22 Mai 2020
strjoin, strcat.
[date time]
MONICA RAWAT
MONICA RAWAT le 22 Mai 2020
both of these function is not working

Connectez-vous pour commenter.

Réponses (1)

Steven Lord
Steven Lord le 22 Mai 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 commentaires
Walter Roberson
Walter Roberson le 19 Avr 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 le 19 Avr 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by