Incorrect Inputformat for datetime function

Hi
I have the following code:0
DateStrings = {'2022-11-08T17:20:00Z'};
t = datetime(DateStrings,'InputFormat','yyyy-MM-dd''T''HH:mm:ss','TimeZone','UTC');
That gives me the following error
Unable to convert '2022-11-08T17:20:00Z' to datetime using the format 'yyyy-MM-dd'T'HH:mm:ss'.
Any idea why?

4 件のコメント

Rik
Rik 2022 年 11 月 9 日
移動済み: Rik 2022 年 11 月 10 日
If you remove the quotes in your input format it should work. Since there are no quotes in your input text, you don't need them.
Steve
Steve 2022 年 11 月 9 日
移動済み: Rik 2022 年 11 月 10 日
It gives me this error when i remove the quotes
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
Steve
Steve 2022 年 11 月 9 日
移動済み: Rik 2022 年 11 月 10 日
If i remove the Z, the code runs fine
Walter Roberson
Walter Roberson 2022 年 11 月 9 日
移動済み: Rik 2022 年 11 月 10 日
the ''T'' inside the InputFormat is the syntax for datetime() to indicate that there is a literal T in the string -- that datetime() should not try to interpret the T as a format code and should match it literally instead.
Actual problem: the user did not put in a format code to match the Z

サインインしてコメントする。

回答 (1 件)

Steven Lord
Steven Lord 2022 年 11 月 9 日

1 投票

If I look at the documentation for the Format property on the datetime documentation page, two of the "Examples of Common Formats" look similar to your date and time data.
'MMMM d, yyyy HH:mm:ss Z' April 9, 2014 21:41:06 -0400
'yyyy-MM-dd''T''HH:mmXXX' 2014-04-09T21:41-04:00
So let's combine them and see what happens. I added the Z from the first of those examples to the end of the format you'd specified in your original code and it seems to have worked.
DateStrings = {'2022-11-08T17:20:00Z'};
t = datetime(DateStrings,'InputFormat','yyyy-MM-dd''T''HH:mm:ssZ','TimeZone','UTC')
t = datetime
08-Nov-2022 17:20:00

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

製品

リリース

R2022b

タグ

質問済み:

2022 年 11 月 9 日

移動済み:

Rik
2022 年 11 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by