Create a numeric variable DATE+HOUR

조회 수: 1 (최근 30일)
David E.S.
David E.S. 2021년 2월 7일
편집: Adam Danz 2021년 2월 7일
Hi. I hope you're ok.
I write this question because I want to create a new numeric variable which contains the date (i.e. 12/09/2019) and the hour (i.e. 01:15:20). I did this in Excel and the result is given in the following photo:
However, If I try to do this in MATLAB (R19b) the result is quite different.
Can you tell me if I'm doing something wrong? I don't know if the sintaxis of the date and hour are correct, and if not, what's wrong with that (I want the result given in my Excel sheet).
Thanks!
David

채택된 답변

Adam Danz
Adam Danz 2021년 2월 7일
편집: Adam Danz 2021년 2월 7일
There's rarely a good reason to use datenum rather than datetime values. See Why are datetime arrays better than datenum?
% Convert to datetime
Time = '01:15:02 12/09/19';
datetime(Time,'InputFormat','HH:mm:ss MM/dd/yy')
ans = datetime
09-Dec-2019 01:15:02
Date numbers in Excel represent the number of days since Jan 1 1900 whereas date numbers in Matlab represent the number of days since Jan 1,0000 so there's a 693960 day difference (see documentation).
If you have the Financial Toolbox you could use x2mdate / m2xdate or you could convert it on your own using the offset mentioned in the documentation.
  댓글 수: 4
David E.S.
David E.S. 2021년 2월 7일
Is it possible to do that conversion from date to number, using the 'ConvertForm', 'excel'?
Thanks!
Adam Danz
Adam Danz 2021년 2월 7일
편집: Adam Danz 2021년 2월 7일
Sure use either of the following syntaxes
Excel supports two date systems that start from 1900 or 1904 which is reason for the second argument.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by