Time stamp formatting question
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Sergio Mendoza
il 17 Giu 2019
Modificato: Adam Danz
il 17 Giu 2019
Hi everyone -
I have a time stamp in the following form : '0d 00:00:5.03000020980835'
I am trying to convert it into a vector in hh:mm:ss, any ideas?
2 Commenti
Walter Roberson
il 17 Giu 2019
Do you need the seconds to come out as 5.03 with no 000020980835 stored?
Adam's suggestion is not bad, but it does end up storing as 00:00:05.030000208 (I might have expected 00:00:05.030000210) . If the 0000208 is not desired, then Adam's code would need to be modified.
Question: is the number of days ever non-zero ?
Risposta accettata
Adam Danz
il 17 Giu 2019
Modificato: Adam Danz
il 17 Giu 2019
dtstr = '0d 00:00:5.03000020980835';
dtstr = regexprep(dtstr,'\d+d',''); %remove days
dt = datetime(dtstr,'InputFormat','HH:mm:ss.SSS','Format','HH:mm:ss')
[update]
To preserve the number of days within the hour-count and to account for missing decimals in the seconds,
dtstr = {'0d 23:59:58.439998626709'
'0d 23:59:59'
'1d 00:00:0'
'1d 00:00:1.44000005722046'
'1d 00:00:2.44000005722046'
'1d 00:00:3.44000005722046'};
% Add .0 to time stamps that are missing the decimal
noDecIdx = ~cellfun(@(x)contains(x,'.'),dtstr);
dtstr(noDecIdx) = cellfun(@(x)[x,'.0'],dtstr(noDecIdx),'UniformOutput',false);
% replace "d " with ":"
dtstr = strrep(dtstr,'d ',':');
% Convert to your desired format as durations
D = duration(dtstr,'InputFormat', 'dd:hh:mm:ss.S','Format','hh:mm:ss');
Result:
6×1 duration array
23:59:58
23:59:59
24:00:00
24:00:01
24:00:02
24:00:03
7 Commenti
Adam Danz
il 17 Giu 2019
Hi Sergio, I updated my answer to address this. Sorry it took some dialog for me to understand what your goal was. My updated answer uses durations rather than datetime.
The updated solution uses durations rather than datetime but it's easy to convert those durations to datetime if that's what you need to do.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Data Type Conversion in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!