datetime unable to parse format 'mmm dd, yyyy'

7 Ansichten (letzte 30 Tage)
Allan Lee
Allan Lee am 22 Jun. 2021
Beantwortet: Walter Roberson am 23 Jun. 2021
I am trying to read a datetime string array with items that looks like this: 'Jan 31, 1990'.
The datetime function was unable to read it with the input format, 'mmm dd, yyyy'.
I did a quick experiment with the following code to convert between datetime and string and encounter the same error:
t_format = 'mmm dd, yyyy';
a_time = datetime(2018, 1, 1)
a_str = datestr(a_time, t_format)
a_time2 = datetime(a_str, 'InputFormat', t_format)
Please let me know if I made any mistake or MATLAB has a bug.
Thanks

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 23 Jun. 2021
datestr() uses 'm' to indicate months, but datetime() uses 'm' to indicate minutes. You cannot use the same format string for both functions.
t_format = 'MMM dd, yyyy';
datetime('Jan 31, 1990', 'InputFormat', t_format)
ans = datetime
31-Jan-1990

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by