Combining two datetime columns into one
    22 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    Autumn P
 am 9 Mär. 2022
  
    
    
    
    
    Kommentiert: Star Strider
      
      
 am 9 Mär. 2022
            Hi
I am trying to combine two columns in a table that are formated as datetime. Date is in format 'mm/dd/yyy' and Time is in format 'HH:mm'
I have tried:
%Obs_data.Timestamp = Obs_data.Date + Obs_data.Time;
Error
Addition is not defined between datetime arrays.
0 Kommentare
Akzeptierte Antwort
  Star Strider
      
      
 am 9 Mär. 2022
        Date = repmat(datetime('now','Format','MM/dd/yyyy'), 3, 1);
Time = datetime('now','Format','HH:mm','Timezone','UTC-7') + hours(0:2).';
Obs_data = table(Date,Time)
Obs_data.Timestamp = Obs_data.Date + timeofday(Obs_data.Time)
Obs_data.Timestamp.Format = 'MM/dd/yyyy HH:mm'
.
2 Kommentare
Weitere Antworten (2)
  David Hill
      
      
 am 9 Mär. 2022
        d=[Obs_data.Date,' ',Obs_data.Time];
datetime(d,'InputFormat','MM/dd/yyyy HH:mm');
2 Kommentare
  David Hill
      
      
 am 9 Mär. 2022
				d=[Obs_data.Date,repmat(' ',size(Obs_data.Date,1),1),Obs_data.Time];%assuming the dates and times are character arrays
%d=[char(Obs_data.Date),repmat(' ',size(Obs_data.Date,1),1),char(Obs_data.Time)]; 
%use above to convert to char arrays if necessary
datetime(d,'InputFormat','MM/dd/yyyy HH:mm');
  Steven Lord
    
      
 am 9 Mär. 2022
        Why is your Time data stored as a datetime array? IMO it would make more sense for you to import or create it as a duration array.
I don't know how to add yesterday and tomorrow in a way that makes sense but I know how to add right now and 6 hours.
rightNow = datetime('now')
h = hours(6)
sixHoursFromNow = rightNow + h % datetime + duration = datetime
If you're reading your time data as text (say from a file) you can convert that to a duration array pretty easily.
h2 = duration('01:23', 'InputFormat', 'hh:mm')
anHour23MinutesFromNow = rightNow + h2
0 Kommentare
Siehe auch
Kategorien
				Mehr zu Dates and Time finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



