DateTime conversion with format string works with one date but not another
7 views (last 30 days)
Show older comments
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')
datetime('10/09/2024 16:58:09.066666','InputFormat','MM/dd/yyyy hh:mm:ss.SSSSSS')
0 Comments
Accepted Answer
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
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)
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')
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')
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)
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)
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.
More Answers (0)
See Also
Categories
Find more on Calendar 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!