# I get wrong logic while I compare two datetimes

조회 수: 5(최근 30일)
Arezoo Ghanbari 2022년 8월 31일
편집: Stephen23 2022년 9월 3일
Hello
Thank you for paying attention to my question.
The format I introduced for the datetimes in my table is ' 'hh:mm:ss dd/MM/uuuu'
but I am wondering why I get the datetime as below. The year must be 2022 instead of 0022.
Besides, when I campare datetimes, the logic is not correct for instance the logic of D has to be 1 but zero is shown instead.
A = datetime
00:43:43 29/06/0022
B = datetime
03:26:53 07/01/0022
C = datetime
08:40:28 07/01/0022
D = isbetween (A,C,B)
D = logical
0
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
Star Strider 2022년 9월 2일

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

### 채택된 답변

dpb 2022년 8월 31일
@Karim has explained why the logic test is correct because your inputs to isbetween are in the wrong order for the result to be true given the values. Note well besides that as you wrote it C > B which is a contradiction of the expected order of input arguments as well. See doc isbetween for all the details on using the function correctly.
What wasn't addressed is the problem about the missing 2000 on the values -- that's indicative that your input data string contained only a 2-digit year and your input format string wasn't set to match in the call to datetime -- but you didn't show us that.
First, try no set input format, let it try and guess...
>> datetime('7/1/22','Format','hh:mm:ss dd/MM/uuuu')
Warning: Successfully converted the text to datetime using the format 'MM/dd/uuuu', but the
format is ambiguous and could also be 'dd/MM/uuuu'. To create datetimes from text with a
specific format call:
datetime(textinput,'InputFormat',infmt)
> In guessFormat (line 109)
In datetime (line 643)
ans =
datetime
12:00:00 01/07/0022
>>
That gives the desired output format, but the year is interpreted literally because it used 'uuuu' as best guess for year.
OK, now let's take its suggestion and try it and see...
>> datetime('7/1/22','InputFormat','M/d/yyyy','Format','hh:mm:ss dd/MM/uuuu')
ans =
datetime
12:00:00 01/07/0022
>>
OK, we got rid of the warning message but (not suprisingly) got the same bogus/unintended result...
Let's try just a two-digit year indicator instead of four...
>> datetime('7/1/22','InputFormat','M/d/uu','Format','hh:mm:ss dd/MM/uuuu')
ans =
datetime
12:00:00 01/07/0022
>>
Nope, still an actual year matched the data...
OK, use 'yy' instead of 'uu'...
>> datetime('7/1/22','InputFormat','M/d/yy','Format','hh:mm:ss dd/MM/uuuu')
ans =
datetime
12:00:00 01/07/2022
>>
And, voila!! SUCCESS!!!
Moral, you get what you ask for...again read the doc at datetime, particularly the 'infmt' descriptions carefully; it explains the details and why it works/worked as it does/did.
##### 댓글 수: 6표시숨기기 이전 댓글 수: 5
Star Strider 2022년 9월 2일

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

### 추가 답변(1개)

Karim 2022년 8월 31일
Note that isbetween(t,tlower,tupper) is used to check the following logic: tlower <= t & t <= tupper.
See below for the code run, and an extra logic check.
TimeFormat = 'hh:mm:ss dd/MM/yyyy';
A = datetime("00:43:43 29/06/2022",'InputFormat',TimeFormat)
A = datetime
29-Jun-2022 00:43:43
B = datetime("03:26:53 07/01/2022",'InputFormat',TimeFormat)
B = datetime
07-Jan-2022 03:26:53
C = datetime("08:40:28 07/01/2022",'InputFormat',TimeFormat)
C = datetime
07-Jan-2022 08:40:28
Note that isbetween(t,tlower,tupper) is used to check the following logic: tlower <= t & t <= tupper
D = isbetween(A,C,B)
D = logical
0
Hence the above is correct... 29 june is not between the two times on 7 january.
D = isbetween(C,B,A)
D = logical
1

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

### 범주

Find more on Calendar in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by