Datetime cannot parse certain literal months

13 views (last 30 days)
I am currently working with date strings. It seems that not all dates can be interpreted using "datetime". E.g.:
datetime('05 Nov 2021','InputFormat','dd MMM yyyy','Locale','en_GB')
works fine, but
datetime('05 Sep 2021','InputFormat','dd MMM yyyy','Locale','en_GB')
throws:
Error using datetime
Unable to convert '05 Sep 2021' to datetime using the format 'dd MMM yyyy' and locale 'en_GB'.
I can not explain what is the matter with "Sep".

Accepted Answer

Stephen23
Stephen23 on 9 Dec 2022
Edited: Stephen23 on 9 Dec 2022
According to american logic, the british three-letter appreviation of September is "sept":
datetime('05 Nov 2021','InputFormat','dd MMM yyyy','Locale','en_GB')
ans = datetime
05-Nov-2021
datetime('05 Sept 2021','InputFormat','dd MMM yyyy','Locale','en_GB')
ans = datetime
05-Sep-2021
PS: to be fair, the DATETIME documentation does not state that abbreviations must be three letters (although I suspect that many english speakers would assume that it does), it actually states "MMM Month, abbreviated name", thus allowing any length abbreviation.
However the documentation does not list those abbreviations anywhere, nor the full months names or anything else, but it does link to the Unicode documentation, where if one spends enough time digging, one might find a reference to how dates are abbreviated in every supported language (I cound not find it).
  3 Comments
Les Beckham
Les Beckham on 9 Dec 2022
Wow, indeed!
Without the Locale specification it errors with the 't' and works without it (in the US anyway)!
I would consider filing a bug report: report a bug
Stephen23
Stephen23 on 9 Dec 2022
Edited: Stephen23 on 11 Mar 2025
"Without the Locale specification it errors with the 't' and works without it (in the US anyway)!"
Because your locale is en_US, which apparently does not use the 't'.
"Where can I find these definitions?"
Here:

Sign in to comment.

More Answers (1)

Jim Benjamin
Jim Benjamin on 11 Mar 2025
Hi everyone,
I have a similar problem: I like to convert the string array s(1) = "11-Mrz-2025 10:03:47" to datetime using
s1 = datetime(s(1), 'InputFormat', 'dd-MMM-yyyy hh:mm:ss', 'Locale', 'de_DE') and I get the error
Error using datetime (line 257)
Unable to convert '11-Mrz-2025 10:03:47' to datetime using the format 'dd-MMM-yyyy hh:mm:ss' and locale 'de_DE'.
I do not know why this Input Format is not working. Thank you
  1 Comment
Stephen23
Stephen23 on 11 Mar 2025
Edited: Stephen23 on 11 Mar 2025
The 'de_DE' abbreviation of the third month is "März":
string(datetime(2025,3,11), 'MMM','de_DE')
ans = "März"
So your "Mrz" will not be matched. While we are here lets take a look at them all:
string(datetime(2025,1:12,11).', 'MMM','de_DE')
ans = 12x1 string array
"Jan" "Feb" "März" "Apr" "Mai" "Juni" "Juli" "Aug" "Sept" "Okt" "Nov" "Dez"
As far as I can tell, the Unicode date names for German are defined here:
Both "März" and "Mär" are defined, but not "Mrz". Lets try them both:
datetime('11März2025', 'Locale','de_DE','InputFormat','ddMMMyyyy')
ans = datetime
11-Mar-2025
datetime('11Mär2025', 'Locale','de_DE','InputFormat','ddMMMyyyy')
Error using datetime (line 257)
Unable to convert '11Mär2025' to datetime using the format 'ddMMMyyyy' and locale 'de_DE'.

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!