Transforming HH:mm:SS to 'dd.MM.yy HH:mm:SS' and loosing seconds
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Butterflyfish
le 22 Juil 2019
Commenté : Butterflyfish
le 22 Juil 2019
>> time = datestr(seconds(duration),'HH:MM:SS')
time =
'02:59:57'
>> date_and_time = datetime(time, 'ConvertFrom', 'datenum', 'Format', 'dd.MM.yy HH:mm:SS')
date_and_time =
datetime
22.07.19 02:59:00
I would like to add current date to the 'time'. Why do I loose the 57 seconds when I do this transformation, and how to do it correctly?
Many thanks!
2 commentaires
Stephen23
le 22 Juil 2019
Modifié(e) : Stephen23
le 22 Juil 2019
I am surprised that this does anything at all, and does not simply throw an error:
>> time = datestr(seconds(duration),'HH:MM:SS') % a date string...
>> date_and_time = datetime(time, 'ConvertFrom', 'datenum',...)
% ^^^^^^^ but here datenum!
Given that the variable time is a character vector (with a date representation), why are you telling datetime that the input is a serial date number?
More importantly, why are you converting a much better duration object to a datestring anyway? If you want a datetime object at the output then I don't see why you need any intermediate (almost) obsolete date strings or date numbers.
Réponse acceptée
Stephen23
le 22 Juil 2019
Modifié(e) : Stephen23
le 22 Juil 2019
"I would like to add current date to the 'time'"
Sure, that is easy, by just adding a duration object to a datetime object.
You do not write what class the very badly-named duration is, but the simplest solution is to add a duration array to the datetime object of the current time:
>> D = seconds(1375.143) % a duration object
D =
1375.1 secs
>> T = datetime() % a datetime object (current time)
T =
22-Jul-2019 13:19:37
>> X = D+T % add them together!
X =
22-Jul-2019 13:42:32
3 commentaires
Steven Lord
le 22 Juil 2019
Stephen Cobeldick has given you a better approach. But to explain why your original approach appeared to lose the seconds, look at the table of values that you can use to create the Format for a datetime in the datetime documentation. In the Format, using the characters s or ss displays the number of whole seconds and using the characters S, SS, ... SSSSSSSSS displays the number of fractional seconds. The case of the S matters.
So you didn't actually lose the seconds, you just were displaying the 0 fractional seconds instead of the 57 whole seconds.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Specifying Target for Graphics Output 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!