Convert Time Given in Seconds to Minute, Seconds, Milliseconds

170 次查看(过去 30 天)
Hi all,
I am trying to convert a time given in seconds such as 183.55s into minute, seconds, milliseconds.
For minute, I'm using: init_mn=fix(init_time/60), which gives me the correct answer.
How can I obtain the seconds and miliseconds without rounding?
Thank you in advance!

回答(4 个)

Star Strider
Star Strider 2021-8-5
This is simply a straightforward format change:
t = seconds(183.55)
t = duration
183.55 sec
t.Format = 'hh:mm:ss.SSS'
t = duration
00:03:03.550
producing the desired result.
.

KSSV
KSSV 2021-8-5

Chunru
Chunru 2021-8-5
[h, m, s] = hms(duration([0 0 183.55]))
h = 0
m = 3
s = 3.5500
% for sec and fractional sec
si = floor(s)
si = 3
sf = s-si
sf = 0.5500

Stephen23
Stephen23 2021-8-5
If you really need minutes (i.e. and not roll-over to hours for >59 minutes) then you can calculate this yourself, e.g.:
inp = 7654.321; % time in seconds
t_min = ceil(1000*inp);
t_ms = mod(t_min,1000) % milliseconds
t_ms = 321
t_min = fix(t_min/1000);
t_sec = mod(t_min,60) % seconds
t_sec = 34
t_min = fix(t_min/60) % minutes
t_min = 127

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by