how to convert date to number

조회 수: 5 (최근 30일)
Mekala balaji
Mekala balaji 2018년 1월 11일
댓글: Mekala balaji 2018년 1월 20일
Hi, I have date as below: 2017/11/28 21:16:42, I want to convert it to number using datenum, but in matlab6p5 it give error that 2017 is too large as month. How can I do this? please help some one.

채택된 답변

Walter Roberson
Walter Roberson 2018년 1월 11일
In MATLAB 6.5, that date format was not supported by datenum(), and that version of datenum() was too early to allow you to pass in a custom date specification.
I suggest that you use sscanf() to break apart the string into numeric pieces, and you can then pass those pieces in to datenum(). Your entries happen to be in the right order so you do not need to re-arrange the numeric parts:
S = '2017/11/28 21:16:42';
datenum( sscanf(S, '%d/%d/%d %d:%d:%d') )
  댓글 수: 11
Walter Roberson
Walter Roberson 2018년 1월 19일
There are two ways you could proceed:
1) https://www.mathworks.com/help/releases/R13sp2/techdoc/ref/etime.html etime() calculates time differences in seconds, which you could then convert to hours; or
2) subtract the two datenum() results to get time differences in days, which you could then convert to hours.
Mekala balaji
Mekala balaji 2018년 1월 20일
It works, thanks, Sir.

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

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by