Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Joseph Cheng
Joseph Cheng am 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 Kommentare
Joseph Cheng
Joseph Cheng am 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.
Ashish
Ashish am 20 Aug. 2014
Thanks Joseph for the detailed explanation. Certainly helped.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by