compare date and time

조회 수: 23 (최근 30일)
Kathy
Kathy 2017년 5월 25일
답변: Peter Perkins 2017년 5월 26일
I need to check of the date is after specific date and time. for example
if (datestr(datenum(datetime))) > (datestr([2017 05 21 12 00 00]
datetime is array of date in the format 22-May-2017 12:00:00

채택된 답변

Star Strider
Star Strider 2017년 5월 25일
I would just use the result of datenum.
Comparing strings will probably not work as you would want it to.

추가 답변 (3개)

Kathy
Kathy 2017년 5월 25일
without the datestr it gives this output for datestr(datenum(datetime)) and (datestr([2017 05 21 12 00 00])
out 7.3683e+05
7.3684e+05
  댓글 수: 1
Star Strider
Star Strider 2017년 5월 25일
The two dates are a day apart, so that seems correct. You can do numerical operations and logical comparisons on the date numbers, including comparisons other than isequal, the only one you can do with strings (using the strcmp function).
Example
date_dif = datenum('22-May-2017 12:00:00') - datenum([2017 05 21 12 00 00])
date_dif =
1
If you want to see the complete date number, type :
format long g
in your Command Window.

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2017년 5월 25일
Don't convert from a datetime into a serial datenum and then to a datestr to compare them. Just compare the datetime variables directly. For instance, with a little nod to Abbott and Costello:
>> pitching = datetime('tomorrow');
>> catching = datetime('today');
>> pitching > catching
ans =
logical
1

Peter Perkins
Peter Perkins 2017년 5월 26일
All you need is
if theDate > '21-May-2017 12:00:00'
That's it. Unless you are using a fairly old version of MATLAB, stay away from datenum and datestr.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by