Datetime conversion from string with non-matching format does not yield an error

I am reading in some data and want to check the format before converting. Interestingly, specifying the InputFormat with 4 digits while only having 2 digits does not yield an error, but rather trails the year with 00:
Reading in a date like
datetime('21.02.12','InputFormat', 'yy.MM.dd')
yields the desired output
datetime
12-Feb-2021
However, explicitly stating a four-digit year
datetime('21.02.12','InputFormat', 'yyyy.MM.dd')
does not produce an error, but rather the output
datetime
12-Feb-0021
Rather than shifting to the first century, I want to get an error message, hinting at the invalid date format. How can I realize this?

 Risposta accettata

Unfortunately, 0021 is a valid date. Rather than rely on datetime to do this, you will likely need to create your own error message.
d=datetime('21.02.12','InputFormat', 'yyyy.MM.dd')
d = datetime
12-Feb-0021
if year(d)<2000
error("Date format is incorrect")
end
Date format is incorrect

5 Commenti

Ok, that means, that datetime automatically trails the years with zeros if 4 digits are expected? It is just unexpected, since datetime expects four digits, only gets two and does not inform about this deviance.
Thank you for the workaround! Have a good day
I wouldn't say "trails", as that implies it adds them to the end. Instead, it prepends the zeros to the number. It is customary to drop the leading zeros. The same thing happens with month and day.
% Displays just what is necessary
d=datetime('21.2.12','InputFormat', 'yyyy.MM.dd', 'Format','yy.M.d')
d = datetime
21.2.12
% Displays the specified number of numbers, prepending w/ 0 if needed
d=datetime('21.2.1','InputFormat', 'yyyy.MM.dd', 'Format','yyyy.MM.dd')
d = datetime
0021.02.01
The point is you have used the wrong input format for your data. Datetime is not designed to validate the input. It is designed to be as robust/flexible as possible to avoid causing errors in your code. If you want input validation that is something else.
The datetime documentation states "yyy, yyyy ... Year, using at least the number of digits specified by the number of instances of'y'"
From that description I would not get the meaning that 'yyyy' will also match years with fewer digits, as it does in fact clearly state that it uses "at least the number of digits specified".
"The same thing happens with month and day."
Although the descriptions in the documentation do not state "at least the number of digits", e.g.:
"MM Month, numerical using two digits"
It seems the there are subtle diffferences between how the format string is interpreted when used to import dates vs. how it is interpreted (and described in the documentation) when used to format dates. It might be flexible when interpreting, but this is not clear from the docs (and as this thread shows).
Does this mean that there is no practical difference between M and MM for interpreting?
Prepending is the better word! Yes, I thought the format would be directly validated, but I can see the point of programming it the way you suggest. Changing the display format is not what I need, since the datevalue itself will still be wrong.
Since the problem only arises with the years, the workaround (year < 2000) is sufficient for me.

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2018b

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by