How do I compare Date-Time objects that have different formats?

3 views (last 30 days)
If I have a datetime array that has the following format:
12/12/2019 3:24:22 AM
12/11/2019 3:24:23 AM
How do I compare it with another datetime array that has this format:
'2017-09-28'
'2017-10-04'
For example, what is the best way to see if an element in one array exists in the other?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 19 May 2020

Since the first array contains a time stamp and the other does not, it is necessary to make sure that one "shifts" the dates in the first array so that they are just the dates themselves and do not have a time stamp associated with it. If you have:

12/11/2019 3:43:22 AM

and

12/11/2019

MATLAB will treat the first one as later. In order to make MATLAB treat them the same, you can use the "dateshift' command to shift the dates with timestamps to to the "start" of the day. You can do this as follows:

>> array = dateshift(array, 'start', 'day');

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!