Transforming HH:mm:SS to 'dd.MM.yy HH:mm:SS' and loosing seconds

1 Ansicht (letzte 30 Tage)
>> 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 Kommentare
Stephen23
Stephen23 am 22 Jul. 2019
Bearbeitet: Stephen23 am 22 Jul. 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.
Butterflyfish
Butterflyfish am 22 Jul. 2019
What I actually would like to do eventually is add a duration to a date and time, to get a date and time output.
E.g.
22.07.19 14:30 + 5400 seconds = 22.07.19 16:00
I thus wanted to formation the duration to the the date to make an addition. I would love to know how to do it in an easier way!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 22 Jul. 2019
Bearbeitet: Stephen23 am 22 Jul. 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 Kommentare
Steven Lord
Steven Lord am 22 Jul. 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.
Butterflyfish
Butterflyfish am 22 Jul. 2019
Ah, that makes even more sense, thanks for this explanation!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Specifying Target for Graphics Output finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by