How can I change the input format of datetime in case that is not modifying.

12 views (last 30 days)
Hi guys
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 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?
attached are the screeshot of my code
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'
  2 Comments
Steven Lord
Steven Lord on 1 Sep 2022
To be explicit, one thing Star Strider noted is that your data ('04:07:06 30/06/0022') doesn't match your specified date format (M/d/yy). I don't know what the 30th month of the year is. Well, I know how it could be interpreted, but more likely than not you meant the 30th day of the 6th month not the 6th day of the 30th month.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 1 Sep 2022
Edited: Star Strider on 1 Sep 2022
Try this —
C = {'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'};
DT = datetime(C, 'InputFormat','HH:mm:ss dd/MM/00yy')
DT = 32×1 datetime array
30-Jun-2022 04:07:06 30-Jun-2022 04:36:51 30-Jun-2022 04:37:03 30-Jun-2022 08:16:22 30-Jun-2022 08:16:52 30-Jun-2022 09:16:52 30-Jun-2022 09:16:59 30-Jun-2022 09:26:52 30-Jun-2022 09:26:53 30-Jun-2022 09:26:59 30-Jun-2022 09:27:33 30-Jun-2022 09:27:38 07-Jan-2022 03:26:22 07-Jan-2022 03:26:53 07-Jan-2022 07:23:53 07-Jan-2022 09:13:54 07-Jan-2022 09:23:53 07-Jan-2022 09:23:53 07-Jan-2022 09:23:58 07-Jan-2022 09:24:00 07-Jan-2022 09:25:00 07-Jan-2022 09:26:00 07-Jan-2022 10:33:57 07-Jan-2022 11:16:44 07-Jan-2022 11:16:54 07-Jan-2022 11:26:44 07-Jan-2022 01:36:54 07-Jan-2022 01:52:06 07-Jan-2022 03:19:24 07-Jan-2022 03:19:54
EDIT — (1 Sep 2022 at 12:00)
To match the original format (with changes) —
DT.Format = 'HH:mm:ss dd/MM/yyyy'
DT = 32×1 datetime array
04:07:06 30/06/2022 04:36:51 30/06/2022 04:37:03 30/06/2022 08:16:22 30/06/2022 08:16:52 30/06/2022 09:16:52 30/06/2022 09:16:59 30/06/2022 09:26:52 30/06/2022 09:26:53 30/06/2022 09:26:59 30/06/2022 09:27:33 30/06/2022 09:27:38 30/06/2022 03:26:22 07/01/2022 03:26:53 07/01/2022 07:23:53 07/01/2022 09:13:54 07/01/2022 09:23:53 07/01/2022 09:23:53 07/01/2022 09:23:58 07/01/2022 09:24:00 07/01/2022 09:25:00 07/01/2022 09:26:00 07/01/2022 10:33:57 07/01/2022 11:16:44 07/01/2022 11:16:54 07/01/2022 11:26:44 07/01/2022 01:36:54 07/01/2022 01:52:06 07/01/2022 03:19:24 07/01/2022 03:19:54 07/01/2022
[DT_Sorted,sortidx] = sort(DT,'ascend')
DT_Sorted = 32×1 datetime array
01:36:54 07/01/2022 01:52:06 07/01/2022 03:19:24 07/01/2022 03:19:54 07/01/2022 03:20:01 07/01/2022 03:20:09 07/01/2022 03:26:22 07/01/2022 03:26:53 07/01/2022 07:23:53 07/01/2022 09:13:54 07/01/2022 09:23:53 07/01/2022 09:23:53 07/01/2022 09:23:58 07/01/2022 09:24:00 07/01/2022 09:25:00 07/01/2022 09:26:00 07/01/2022 10:33:57 07/01/2022 11:16:44 07/01/2022 11:16:54 07/01/2022 11:26:44 07/01/2022 04:07:06 30/06/2022 04:36:51 30/06/2022 04:37:03 30/06/2022 08:16:22 30/06/2022 08:16:52 30/06/2022 09:16:52 30/06/2022 09:16:59 30/06/2022 09:26:52 30/06/2022 09:26:53 30/06/2022 09:26:59 30/06/2022
sortidx = 32×1
27 28 29 30 31 32 13 14 15 16
Use the ‘sortidx’ vector as an index to match other variables, if necessary. (It would be the first index in a mulit-dimensional array.)
.

Categories

Find more on Dates and Time 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!