Add colon in between 6 digit numbers to represent time
4 views (last 30 days)
Show older comments
Hi guys,
Hopefully this is an easy one for some but can't figure out a way myself. I have 6 digit numbers and want to add colons in between every two digits, to represent time.
For example, I have 134911 and want to make it 13:49:11 (in hh:mm:ss), which will allow me to estimate time intervals for some processing I perform later on.
Any help, would be much appreciated!.
Thank you
3 Comments
Stephen23
on 3 Feb 2019
Storing this in a single integer misrepresents the information stored in a date string. It is easier to parse this character vector/string directly:
>> info.AcquisitionTime = '134911';
>> sprintf('%c%c:%c%c:%c%c',info.AcquisitionTime)
ans = 13:49:11
Accepted Answer
Star Strider
on 2 Feb 2019
One approach:
Time = 134911;
Tr = [fix(Time/1E+4) fix(Time/1E+2)-1E+2*fix(Time/1E+4) rem(Time, 1E+2)];
Tstr = sprintf('%02d:%02d:%02d', Tr)
producing:
Tstr =
'13:49:11'
There are likely ways to get the date and time functions to do this as well.
0 Comments
More Answers (1)
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!