How to convert from decimal time to hh:mm:ss
Show older comments
I am using xlsread to read an excel spreadsheet. One of the values that it is reading is time. Inside the spreadsheet the time is in the correct format (hh:mm:ss), however the value that xlsread returns for the time is a decimal. Does anyone know how to convert this value to the hh:mm:ss format in either MATLAB or C++?
Thanks!
Accepted Answer
More Answers (2)
Microsoft has bee so smart to invent two different serial date numbers: One for Windows and one for Macs.
if ismac
MatlabDate = datestr(ExcelDateNum + 695422, 13);
else
MatlabDate = datestr(ExcelDateNum + 693960, 13);
end
Unfortunately this is very wrong, because it does not matter, if the current computer is a Mac, but if the Excel file was stored on a Mac. But I do not know a method to find out this detail.
Peter Perkins
on 29 Nov 2017
Unless you are using an older version of MATLAB, you are likely better off using readtable (instead of xlsread) and datetime (instead of datestr and datenum). Even if you use xlsread to read Excel serial date numbers, you can use
dates = datetime(excelDates,'ConvertFrom','excel') % or 'excel1904'
2 Comments
Tunga Rangaswamy
on 5 Dec 2018
Edited: Tunga Rangaswamy
on 5 Dec 2018
This works great! It produces date in day-month-year format and time in hh:mm:ss format. How do I get time in hh:mm:ss.mmm (including milliseconds)?
Peter Perkins
on 11 Dec 2018
Assuming you mean "the time portion of a datetime", you'd need to set the Format property to include HH:mm:ss.SSS. Look at the documentation for datetime.Format.
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!