Problem with datetime in German

조회 수: 58 (최근 30일)
Joshua
Joshua 2021년 12월 3일
답변: Eric Sofen 2021년 12월 3일
From a German language spreadsheet I have the following DateString: '21.Okt.2020'
where Okt is short for October.
How to read this as a datetime? According to the documentation the following should work, but unfortunately doesn't:
datetime('21.Okt.2020','Locale','de_DE','InputFormat','dd.MMM.yyyy')
Any advice would be greatly appreciated.
Regards

채택된 답변

Stephen23
Stephen23 2021년 12월 3일
There does seem to be a problem with DATETIME handling the period characters (below).
Workaround: replace/remove the period characters, as per my first two examples.
datetime('21Okt2020','Locale','de_DE','InputFormat','ddMMMyyyy')
ans = datetime
21-Oct-2020
datetime('21-Okt-2020','Locale','de_DE','InputFormat','dd-MMM-yyyy')
ans = datetime
21-Oct-2020
datetime('21.Okt.2020','Locale','de_DE','InputFormat','dd.MMM.yyyy')
Error using datetime (line 651)
Unable to convert '21.Okt.2020' to datetime using the format 'dd.MMM.yyyy' and locale 'de_DE'.
  댓글 수: 2
Stephen23
Stephen23 2021년 12월 3일
In case anyone suggests to put the period character inside single quotes:
datetime('21.Okt.2020','Locale','de_DE','InputFormat','dd''.''MMM''.''yyyy')
Error using datetime (line 651)
Unable to convert '21.Okt.2020' to datetime using the format 'dd'.'MMM'.'yyyy' and locale 'de_DE'.
Joshua
Joshua 2021년 12월 3일
Thanks for the answer!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Eric Sofen
Eric Sofen 2021년 12월 3일
We’re aware of the issues with parsing month abbreviations in some locales, and we're looking at ways to improve this. In this case, the "." following Okt is treated as part of the month abbreviation by the internationalization library we use. For now, the datetime format that works to parse the timestamp is:
datetime('21.Okt.2020','Locale','de_DE','InputFormat','dd.MMMyyyy')
(Notice no period after MMM.)

Walter Roberson
Walter Roberson 2021년 12월 3일
Observe:
datetime('21.Oct.2020', 'InputFormat', 'dd.MMM.yyyy', 'Format', 'dd.MMM.yyyy')
ans = datetime
21.Oct.2020
So using dots is possible for English.
datetime('21-Okt-2020', 'Locale', 'de_DE', 'InputFormat', 'dd-MMM-yyyy', 'Format', 'dd.MMM.yyyy')
ans = datetime
21.Oct.2020
And using dashes is okay for German
datetime('21.Okt-2020', 'Locale', 'de_DE', 'InputFormat', 'dd.MMM-yyyy', 'Format', 'dd.MMM.yyyy')
ans = datetime
21.Oct.2020
and dot between the day and the month is ok in German
datetime('21.Okt.2020', 'Locale', 'de_DE', 'InputFormat', 'dd.MMM.yyyy', 'Format', 'dd.MMM.yyyy')
Error using datetime (line 651)
Unable to convert '21.Okt.2020' to datetime using the format 'dd.MMM.yyyy' and locale 'de_DE'.
But dot between the year and the month is not okay in German.
I do not know why this is... but see the following
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 12월 3일
편집: Walter Roberson 2021년 12월 3일
datetime('21.Okt..2020', 'Locale', 'de_DE', 'InputFormat', 'dd.MMM.yyyy', 'Format', 'dd.MMM.yyyy')
ans = datetime
21.Oct.2020
I do not know quite what to make of this.
Joshua
Joshua 2021년 12월 3일
@Walter Roberson thanks for your effort, does indeed seem like an issue for Mathworks. In my case I have a bunch of different dates with often different formating. In the end I ended up parsing and replacing the '.' with '-' as both you and @Stephen suggested.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by