How to count the number of hours with reference to a MATLAB formatted date?

1 view (last 30 days)
Using MATLAB datenum function, one can generate date patterns like 7.3560e+05. This represents 03-Jan-2014 02:00:00.
If I want to count the number of hours (for e.g. in this case: 50 hrs) of this number from 01-Jan-2013 00:00:00, how can I do it? I want to generalize for any datenum formatted date.
Thanks.

Accepted Answer

Joseph Cheng
Joseph Cheng on 20 Aug 2014
the number returned from datenum is in days. so if we go
time1 = datenum(datestr(now));
pause(1);
time2 = datenum(datestr(now));
deltaTime = (time2-time1)*24*60*60
then we should get deltaTime = 1 (second). So to count the hours we can go
time1 = datenum(datestr(now));
current_time = datestr(time1)
twodayslater = datestr(time1+50/24)
such that the 50 hours later is 2days 2 hours so we can see that when comparing the current_time to twodayslater.
  2 Comments
Joseph Cheng
Joseph Cheng on 20 Aug 2014
so for your arbitrary datestr formatted date you can go
datestr(datenum(' 01-Jan-2013 00:00:00') +50/24)
As long as your date string is accepted into datenum you can go back and forth.

Sign in to comment.

More Answers (0)

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!