compare date and time
23 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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.
0 件のコメント
その他の回答 (3 件)
Kathy
2017 年 5 月 25 日
1 件のコメント
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
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
0 件のコメント
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!