# How to find the minimum and maximum value of timestamp (hh:mm)?

131 views (last 30 days)
Ishaan Kochhar on 13 Apr 2018
Edited: Peter Perkins on 19 Apr 2018
I have a matrix called time_stamps which contain several time stamps for twitter data (when the tweet was posted). time_stamps is a 1645x5 char array right now. It contains 5 columns (12:44 is a time stamp with 5 characters). With this, I am trying to find the minimum time stamp and the maximum one. Which time is the least and which time is the most? I'm having trouble writing the code to do it though. Any help you can give would be much appreciated.
time_stamps = table2array(tunedData(:,4)); %the 4th column indicates time stamps
time_stamps = cell2mat(time_stamps);
%now i need to find the minimum and maximum of these time stamps

Walter Roberson on 13 Apr 2018
time_stamps = datetime(tunedData{:,4}, 'InputFormat', 'HH:mm');
min(time_stamps)
max(time_stamps)

Peter Perkins on 13 Apr 2018
Also, beginning in R2018a, you can convert "pure time" text timestamps to duration, so
time_stamps = duration(tunedData{:,4}, 'InputFormat', 'HH:mm');
min(time_stamps)
max(time_stamps)
Walter Roberson on 13 Apr 2018
Looks like it needs to be 'hh:mm' instead of 'HH:mm'. Which is inconsistent with datetime()
>> duration('13:48', 'InputFormat', 'hh:mm')
ans =
duration
13:48:00
>> duration('13:48', 'InputFormat', 'HH:mm')
Error using duration (line 286)
Unsupported format 'HH:mm'. See the documentation of 'InputFormat' for valid formats.
>> datetime('13:48', 'InputFormat', 'HH:mm')
ans =
datetime
13-Apr-2018 13:48:00
>> datetime('13:48', 'InputFormat', 'hh:mm')
Error using datetime (line 616)
Unable to parse '13:48' as a date/time using the format 'hh:mm'.
datetime() needs HH for hours beyond 12 and treats hour '12' as 00 when 'hh' is used, but duration() rejects 'HH' as being invalid. :(