www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/CompareDatesAndTimeExample.m

    %% Compare Dates and Time
% This example shows how to compare |datetime| and |duration| arrays.
% You can perform an element-by-element comparison of values in two
% |datetime| arrays or two |duration| arrays using relational operators,
% such as |>| and |<|.
%% Compare Datetime Arrays
% Compare two |datetime| arrays. The arrays must be the same size or one
% can be a scalar.
A = datetime(2013,07,26) + calyears(0:2:6)
B = datetime(2014,06,01)
%%
A < B 
%%
% The |<| operator returns logical |1| (true) where a datetime in |A|
% occurs before a datetime in |B|.
%%
% Compare a |datetime| array to text representing a date.
A >= 'September 26, 2014'
%%
% Comparisons of |datetime| arrays account for the time zone information of
% each array. 
%%
% Compare September 1, 2014 at 4:00 p.m. in Los Angeles with 5:00
% p.m. on the same day in New York.
A = datetime(2014,09,01,16,0,0,'TimeZone','America/Los_Angeles',...
    'Format','dd-MMM-yyyy HH:mm:ss Z')
%%
B = datetime(2014,09,01,17,0,0,'TimeZone','America/New_York',...
    'Format','dd-MMM-yyyy HH:mm:ss Z')
%%
A < B
%%
% 4:00 p.m. in Los Angeles occurs after 5:00 p.m. on the same day in New York.
%% Compare Durations
% Compare two |duration| arrays.
A = duration([2,30,30;3,15,0])
B = duration([2,40,0;2,50,0])
%%
A >= B
%%
% Compare a duration array to a numeric array. Elements in the numeric
% array are treated as a number of fixed-length (24-hour) days.
A < [1; 1/24]
%% Determine if Dates and Time Are Contained Within an Interval
% Use the |isbetween| function to determine whether values in a |datetime|
% array lie within a closed interval.
%%
% Define endpoints of an interval.
tlower = datetime(2014,08,01)
tupper = datetime(2014,09,01)
%%
% Create a |datetime| array and determine whether the values lie within the
% interval bounded by |t1| and |t2|.
A = datetime(2014,08,21) + calweeks(0:2)
%%
tf = isbetween(A,tlower,tupper)