How do I convert a decimal number to a time?

How would I convert a number such as 7.8 into a time in HH:MM? 7.8 is 7.8 hours from the start of the day, so should be shown as 07:48.

2 Comments

Well, that depends on the unit of 7.6342
Hi jonas, sorry yeah I forgot to mention that, I was updating my question as you replied hahah

Sign in to comment.

 Accepted Answer

jonas
jonas on 2 Aug 2018
Edited: jonas on 2 Aug 2018
Alternatively, if you just want to display amount of hours and minutes.
duration(hours(7.8),'format','hh:mm')
ans =
duration
07:48

5 Comments

Thank you, this is just what I was looking for :)
Now you can work with the duration to add it to another time, build a vector, or convert it to another unit of time!
Equivalently:
>> t = hours(7.8)
t =
duration
7.8 hr
>> t.Format = 'hh:mm'
t =
duration
07:48
If I want the opisite from 07:48 to 7.8?

Sign in to comment.

More Answers (1)

datestr(hours(7.8),'HH:MM');
Result
>> datestr(hours(7.8),'HH:MM')
ans =
07:48

4 Comments

Hi Kalyan, sorry, I was updating my question as you replied, 7.8 hours should be 7.8 hours from the beginning of the day, so should show as 07:48. Using your solution I get the time as 19:12.
Updated answer datestr(hours(7.8),'HH:MM');
This is the old way to to it (before R2014b). In more recent releases, the duration approach is better.
Thanks for the information.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!