How do I turn an integer into clock time

6 Ansichten (letzte 30 Tage)
Stephen De Leon
Stephen De Leon am 28 Mär. 2020
Bearbeitet: dpb am 31 Mär. 2020
How do I convert or code so from a given input of 658 I get the clock time of 6:58. So that it would be something like this:
Input time based on a 24-hour clock: 658
You entered 6:58
The equivalent time based on a 12-hour clock is 6:58 AM
  2 Kommentare
Walter Roberson
Walter Roberson am 28 Mär. 2020
See mod()
dpb
dpb am 28 Mär. 2020
Amplifying on Walter's hint...
timestr=@(n) sprintf('Clock time: %2d:%02d',fix(n/100),mod(n,100));
>> timestr(658)
ans =
'Clock time: 6:58'
>>
Salt to taste...

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

SaiDileep Kola
SaiDileep Kola am 31 Mär. 2020
Bearbeitet: dpb am 31 Mär. 2020
In addition to the hints given, this would entirely solve your problem
function Time = findclocktime(I)
flag = 'am';
if(I >= 1300)
I = I - 1200;
flag = 'pm';
end
Time = sprintf('Clock time: %2d:%02d %s',fix(I/100),mod(I,100),flag);
end

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by