I get wrong logic while I compare two datetimes

조회 수: 10 (최근 30일)
Arezoo Ghanbari
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.
would you please guide me.
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
Rik
Rik 2022년 9월 1일
@Arezoo Ghanbari flags are meant to attract the attention of admins. If you think an answer is good, please consider giving it an upvote (if you haven't done so already) and comment with a thank you.

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

채택된 답변

dpb
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
Walter Roberson
Walter Roberson 2022년 9월 2일
Huh, I would have guessed it would be necessary to quote the 00.
txt = '04:07:06 30/06/0022';
dtm = datetime(txt,'inputformat',"HH:mm:ss dd/MM/'00'yy")
dtm = datetime
30-Jun-2022 04:07:06

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

추가 답변 (1개)

Karim
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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by