MATLAB: Converting time/dates to hours or number

arithmeticconvertion time datesdatestime

Hi I'm getting difficult to convert time dates to hours so i can set paying in my bill parking program . if i have 2 dates like this
t1 = datestr(datenum(now)); %answer is 13-Sep-2016 16:00:49
t2 = datestr(datenum(now)); %answer is 13-Sep-2016 20:00:14
i want to arithmetic (t2-t1) so i can get variable in hour or number . Please Help , Thanks b4 . I'm using matlab R2008a

Best Answer

Do not try to do date arithmetic on datestr . Do date arithmetic on the datenum values. The result will be in days. You can use datevec() or datestr() or plain arithmetic to convert the day difference to whatever form you prefer.
(Side note: now() returns a datenum directly.)