MATLAB Answers

how to convert date to number

108 views (last 30 days)
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.


Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 11 Jan 2018
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') )


Show 8 older comments
Mekala balaji
Mekala balaji on 19 Jan 2018
Sir, it works,
Sir one more help needed.
I want to calculate time elapsed(cell array):
T1 2017/9/2 21:16:42
T2 2017/9/17 11:34:2
T3 2017/10/23 16:15:23
T4 2017/11/28 22:23:17
T5 2018/1/6 2:21:56
T6 2018/1/13 14:37:33
Current time: 2018/1/13 20:24:22
I want to calculate time difference in hours between "Current time" to T1, T2, T3, T4, T5, T6
Walter Roberson
Walter Roberson on 19 Jan 2018
There are two ways you could proceed:
1) 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 on 20 Jan 2018
It works, thanks, Sir.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by