I get wrong logic while I compare two datetimes
4 ビュー (過去 30 日間)
古いコメントを表示
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
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
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
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")
Star Strider
2022 年 9 月 2 日
I posted it yesterday in How can I change the input format of datetime in case that is not modifying.
その他の回答 (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)
B = datetime("03:26:53 07/01/2022",'InputFormat',TimeFormat)
C = datetime("08:40:28 07/01/2022",'InputFormat',TimeFormat)
Note that isbetween(t,tlower,tupper) is used to check the following logic: tlower <= t & t <= tupper
D = isbetween(A,C,B)
Hence the above is correct... 29 june is not between the two times on 7 january.
D = isbetween(C,B,A)
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!