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

1 回表示 (過去 30 日間)
Butterflyfish
Butterflyfish 2019 年 7 月 22 日
コメント済み: Butterflyfish 2019 年 7 月 22 日
>> 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 件のコメント
Stephen23
Stephen23 2019 年 7 月 22 日
編集済み: Stephen23 2019 年 7 月 22 日
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 2019 年 7 月 22 日
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!

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2019 年 7 月 22 日
編集済み: Stephen23 2019 年 7 月 22 日
"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 件のコメント
Steven Lord
Steven Lord 2019 年 7 月 22 日
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 2019 年 7 月 22 日
Ah, that makes even more sense, thanks for this explanation!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by