How to convert from decimal time to hh:mm:ss

조회 수: 11 (최근 30일)
Caroline
Caroline 2013년 7월 29일
댓글: John Rodriguez 2019년 2월 21일
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!

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 7월 29일
datestr(yournumber,'HH:MM:SS')
  댓글 수: 3
Mohamed Nedal
Mohamed Nedal 2017년 11월 22일
Hello Azzi, I've tried your answer on this time ( 2637 minutes) but I've got
00:00:00
Could you please tell me how to convert this time in this case?
John Rodriguez
John Rodriguez 2019년 2월 21일
I'm getting the same result. My data is in the format HHMMSS and I want it to go to HH:MM:SS. I'm now just getting 00:00:00 for my full array.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Jan
Jan 2013년 7월 29일
편집: Jan 2013년 7월 29일
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
Peter Perkins 2017년 11월 29일
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
Tunga Rangaswamy
Tunga Rangaswamy 2018년 12월 5일
편집: Tunga Rangaswamy 2018년 12월 5일
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
Peter Perkins 2018년 12월 11일
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.

댓글을 달려면 로그인하십시오.

카테고리

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