Datetime cannot parse certain literal months

12 次查看(过去 30 天)
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".

采纳的回答

Stephen23
Stephen23 2022-12-9
编辑:Stephen23 2022-12-9
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 个评论
Les Beckham
Les Beckham 2022-12-9
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 2022-12-9
编辑:Stephen23 2025-3-11
"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:

请先登录,再进行评论。

更多回答(1 个)

Jim Benjamin
Jim Benjamin 2025-3-11
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 个评论
Stephen23
Stephen23 2025-3-11
编辑:Stephen23 2025-3-11
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'.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by