Adding hours to time

조회 수: 5 (최근 30일)
Kwan Zhao Jie
Kwan Zhao Jie 2018년 4월 17일
댓글: Kwan Zhao Jie 2018년 4월 17일
In my code, i want to state the current time and the time 8 hours later.
function pushbutton8_Callback(hObject, eventdata, handles)
t1 = datestr(now,'HH:MM Am')
t2 = datestr(now,'HH:MM Am') + 8
set(handles.Impt, 'String', sprintf('Your Working Hours will be from %s - %s\n', t1, t2));
the time that is shown in T1 is correct, however im unable to get the T2 value that i wanted. Im currently using MATLAB R2013B.

채택된 답변

Walter Roberson
Walter Roberson 2018년 4월 17일

datestr() returns a character vector. When you add 8 to it, you are taking the encoding of each character and adding 8 to that '0' becomes 56 ('8'), '1' becomes 57 ('9'), '2' becomes 58 (':'), '3' becomes 59 (';'), ... '9' becomes 65 ('A'), ':' becomes 66 ('B')..

If you want to add 8 hours:

datestr(addtodate(datenum(t1),8,'h'),'HH:MM Am')

You should also consider using datevec instead:

datestr(datevec(t1) + [0 0 0 8 0 0], 'HH:MM Am')

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by