DateTime conversion with format string works with one date but not another

13 views (last 30 days)
The first conversion works without error. The second throws an error. I'm not seeing why. Any insight would be appreciated. Thanks.
datetime('10/07/2024 02:52:00.000000','InputFormat','MM/dd/yyyy hh:mm:ss.SSSSSS')
ans = datetime
07-Oct-2024 02:52:00
datetime('10/09/2024 16:58:09.066666','InputFormat','MM/dd/yyyy hh:mm:ss.SSSSSS')
Error using datetime (line 257)
Unable to convert '10/09/2024 16:58:09.066666' to datetime using the format 'MM/dd/yyyy hh:mm:ss.SSSSSS'.

Accepted Answer

dpb
dpb on 9 Oct 2024
From the "infmt" link for datetime...
hh Hour, 12-hour clock notation using two digits
HH Hour, 24-hour clock notation using one or two digits
You're trying to read a 24-hr date with a 12-hr format string...
datetime It's awfully inconvenient because it takes several clicks to follow the links to <the full format table> but you have to go digging to be sure you've got the right one for the given format being used.
Also, it is picky that it will only convert one specific format in a single call; even if you let it diagnose the format on its own, it uses whatever it decides is correct on the first guess and if a later one is different, it fails.
  3 Comments
Steven Lord
Steven Lord on 9 Oct 2024
Also, it is picky that it will only convert one specific format in a single call; even if you let it diagnose the format on its own, it uses whatever it decides is correct on the first guess and if a later one is different, it fails.
For a certain definition of "fails" you're correct.
S = ["10/07/2024 02:52:00.000000", "10/09/2024 16:58:09.066666"];
dt1 = datetime(S)
Warning: Successfully converted the text to datetime using the format 'MM/dd/uuuu HH:mm:ss.SSS', but the format is ambiguous and could also be 'dd/MM/uuuu HH:mm:ss.SSS'. To create datetimes from text with a specific format call:

datetime(textinput,'InputFormat',infmt)
dt1 = 1x2 datetime array
07-Oct-2024 02:52:00 09-Oct-2024 16:58:09
In that case it deduced the format basically correctly (using HH instead of hh) though as the warning stated it wasn't sure if the month or the date was first. If you specify a format that doesn't work with all the elements of the first input argument, it doesn't warn but returns NaT.
dt2 = datetime(S, 'InputFormat','MM/dd/yyyy hh:mm:ss.SSSSSS')
dt2 = 1x2 datetime array
07-Oct-2024 02:52:00 NaT
Then you can convert the elements of S corresponding to NaT to datetime using the other format.
dt2(isnat(dt2)) = datetime(S(isnat(dt2)),'InputFormat','MM/dd/yyyy HH:mm:ss.SSSSSS')
dt2 = 1x2 datetime array
07-Oct-2024 02:52:00 09-Oct-2024 16:58:09
dpb
dpb on 9 Oct 2024
Agreed; wasn't harping, just pointing out that one must deal with one format at a time; the above works to use 'HH' instead of 'hh'; if the day field also happened to have a value >12 in it, then even it would turn out to not be ambiguous in this case.
S = ["10/07/2024 02:52:00.000000", "10/09/2024 16:58:09.066666", "10/29/2024 16:58:09.066666"];
dt1 = datetime(S)
dt1 = 1x3 datetime array
07-Oct-2024 02:52:00 09-Oct-2024 16:58:09 29-Oct-2024 16:58:09
without any warnings or messages.
I haven't tested recently; istr that once upon a time if one had a really, really long input array that had a value way down that was the first one of the other type that it would fail instead of going on and returning NaT for the offenders, but that may well be past behavior now. The datetime was pretty green when it was first introduced(*) and I'm so old that it still seems like new stuff and I may be recalling some behavior that hasn't been around for quite a long time now... :)
(*) I see that was in R2014b, that's now 10-yr ago and its support is much more extensive and warts are much fewer now than then...
But,
S = ["10/07/2024 02:52:00","10/09/2024 16:58:09","10/29/2024 16:58:09","13/1/2025 16:58:09"];
datetime(S)
Error using datetime (line 257)
Could not recognize the format of the date/time text. You can specify a format using the 'InputFormat' parameter. If the date/time text contain day, month, or time zone names in a language foreign to the 'en_US' locale, those might not be recognized. You can specify a different locale using the 'Locale' parameter.
If one gets a dataset that has some in one format and some in another...it throws up its hands. Unfortunately, one finds such things all too frequently when collecting data from outside sources.
That, of course, is not MATLAB's fault and not intending to imply it's anything datetime can do anything about, just that one has to deal with the input either first or last to be consistent with what is intended.

Sign in to comment.

More Answers (0)

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!