D = {'01/01/2014' '12:00:00' 'AM'; ...
'01/01/2014' '06:00:00' 'AM'; ...
'01/01/2014' '10:00:00' 'AM'; ...
'01/01/2014' '12:00:00' 'PM'; ...
'09/04/2014' '11:00:00' 'PM'};
DateV = datevec(strcat(D(:, 1), '#', D(:, 2)), 'mm/dd/yyyy#HH:MM:SS');
IsPM = strcmpi(D(:, 3), 'PM');
pstPM = (DateV(:, 4) < 12 & IsPM);
DateV(pstPM, 4) = DateV(pstPM, 4) + 12;
midAM = (DateV(:, 4) == 12 & ~IsPM);
DateV(midAM, 4) = 0;
DateN = datenum(DateV);
Now the time difference can be calculated by a simple subtraction.
datevec(DateN(end) - DateN(1))
[EDITED 2] No, the conversion of the time difference to months and years is not meaningful: It is not clear if the "month" has 28, 29, 30 or 31 days, if it concerns the difference between dates. Better:
Duration = DateN(end) - DateN(1);
DurationDays = fix(Duration);
Tmp = rem(Duration, 1);
DurationHour = floor(Tmp * 24);
DurationMin = floor(rem(Tmp * 1440, 60));
DurationSec = round(rem(Tmp * 86400, 60));
Sorry, this is too ugly! Nicer:
D = {'02/14/2014', '10:02:04', 'AM'; ...
'02/26/2014', '01:00:00', 'PM'};
DateN = datenum(strcat(D(:, 1), '#', D(:, 2), '#', D(:, 3)), ...
'mm/dd/yyyy#HH:MM:SS#AM');
Duration = DateN(end) - DateN(1);
DDays = fix(Duration);
[~,~,~, Dhour, Dmin, Dsec] = datevec(rem(Duration, 1));
Best Answer