I get wrong logic while I compare two datetimes

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 Comments

caution, hh is 12 hour clock not 24 hour
@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.

Sign in to comment.

 Accepted Answer

@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 Comments

Thank you,
I have two columns in my table, Date and time
Matlab has taken Date as datetime format automatically, and the format M/d/yy does not work for that. it still returns year as 0022 for Date. How can I possible change the format of date in case that it is taken by default as datetime?
another issue : as you see the order of months also is changed. for instance january comes after september instead of July. that is why isbetween does not work.
would you please guide me
'04:07:06 30/06/0022'
'04:36:51 30/06/0022'
'04:37:03 30/06/0022'
'08:16:22 30/06/0022'
'08:16:52 30/06/0022'
'09:16:52 30/06/0022'
'09:16:59 30/06/0022'
'09:26:52 30/06/0022'
'09:26:53 30/06/0022'
'09:26:59 30/06/0022'
'09:27:33 30/06/0022'
'09:27:38 30/06/0022'
'03:26:22 07/01/0022'
'03:26:53 07/01/0022'
'07:23:53 07/01/0022'
'09:13:54 07/01/0022'
'09:23:53 07/01/0022'
'09:23:53 07/01/0022'
'09:23:58 07/01/0022'
'09:24:00 07/01/0022'
'09:25:00 07/01/0022'
'09:26:00 07/01/0022'
'10:33:57 07/01/0022'
'11:16:44 07/01/0022'
'11:16:54 07/01/0022'
'11:26:44 07/01/0022'
'01:36:54 07/01/0022'
'01:52:06 07/01/0022'
'03:19:24 07/01/0022'
'03:19:54 07/01/0022'
'03:20:01 07/01/0022'
'03:20:09 07/01/0022'
Well, when you pass four-digit year strings with explicit '00' century digits, what else is datetime to think but that you gave it an actual year? These data are simply wrong, not just formatted incorrectly; it's the input file that is wrong here, not datetime.
Secondly, the date string '04:07:06 30/06/0022' canNOT be ,'HH:mm:ss MM/dd/yyyy' because that would imply a month of 30 -- this is backwards of your earlier description of the month/day field order in your original post which had no data to prove otherwise in it -- all day and months field values were <12 so couldn't prove your description was wrong.
You'll have to fix up the data going in or correct the result afterwards to get these correct...
tstring={'04:07:06 30/06/0022'};
tstring=strrep(tstring,'/00','/20');
datetime(tstring,'inputformat','HH:mm:ss dd/MM/yyyy')
ans = datetime
30-Jun-2022 04:07:06
The best way to solve this problem would be to fix whatever is the source of the data to not produce ill-formed date strings to try then read.
One presumes the last issue about Jan and Sept order is owing entirely to having swapped the day/month fields before; isbetween works correctly for the dates it is given; your dates were not what you intended them to be.
Another approach to handling those badly-formatted years (from here):
txt = '04:07:06 30/06/0022';
dtm = datetime(txt,'inputformat','HH:mm:ss dd/MM/00yy')
dtm = datetime
30-Jun-2022 04:07:06
Hmmm...how did you dream up that one, @Stephen23? :) I'd've not thunk of that in E6 years although it makes perfect sense once see it.
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

Sign in to comment.

More Answers (1)

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

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Asked:

on 31 Aug 2022

Edited:

on 3 Sep 2022

Community Treasure Hunt

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

Start Hunting!