Lable with a duration

8 Ansichten (letzte 30 Tage)
TheDice
TheDice am 4 Mai 2021
Kommentiert: Steven Lord am 4 Mai 2021
Hi, I have a duration here that I would like to display as a running down number in a label or message box. But I don't know how to convert a duration.
C = DateTimeNow - DateTimeStart;
Many greetings
Fabian
  3 Kommentare
William Rose
William Rose am 4 Mai 2021
@TheDice Fabian,
DateString=datestr(C,'HH:MM:S')
gives a string with the hours, minutes, seconds of c.
William Rose
William Rose am 4 Mai 2021
Fabian @TheDice See help on datestr() for more formatting examples.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Scott MacKenzie
Scott MacKenzie am 4 Mai 2021
You don't "convert" a duration. You control the format via the 'format' property. Try...
t1 = datetime;
pause(1);
t2 = datetime;
z = t2 - t1;
s1 = sprintf('%s', duration(z, 'format', 's'));
s2 = sprintf('%s', duration(z, 'format', 'hh:mm:ss'));
fprintf('Ex1: %s\n', s1);
fprintf('Ex2: %s\n', s2);
Output:
Ex1: 1.009 sec
Ex2: 00:00:01
Of course, there are other possibilities. Try doc duration for all the details.
  1 Kommentar
Steven Lord
Steven Lord am 4 Mai 2021
sprintf is one way to create a char vector or a string from a duration. string is another way to create a string from a duration.
timeSinceMidnight = datetime('now')-datetime('today')
timeSinceMidnight = duration
16:33:25
S = string(timeSinceMidnight)
S = "16:33:25"
The new formattedDisplayText function introduced in release R2021a is another, though you may have to trim it of leading and trailing whitespace.
T = formattedDisplayText(timeSinceMidnight)
T =
" 16:33:25 "

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Develop Apps Using App Designer 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