Transforming HH:mm:SS to 'dd.MM.yy HH:mm:SS' and loosing seconds
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Butterflyfish
el 22 de Jul. de 2019
Comentada: Butterflyfish
el 22 de Jul. de 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 comentarios
Stephen23
el 22 de Jul. de 2019
Editada: Stephen23
el 22 de Jul. de 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.
Respuesta aceptada
Stephen23
el 22 de Jul. de 2019
Editada: Stephen23
el 22 de Jul. de 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 comentarios
Steven Lord
el 22 de Jul. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Specifying Target for Graphics Output en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!