필터 지우기
필터 지우기

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

조회 수: 5 (최근 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 CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by