Trouble parsing ISO8601 date strings into datetime

23 ビュー (過去 30 日間)
Klynton
Klynton 2025 年 1 月 23 日 18:22
コメント済み: Klynton 2025 年 1 月 27 日 12:36
Hi there I am trying to take some datasets with what I believe are ISO8601 timecodes and convert the timecodes to datetime for plotting and sorting by date and time. I think it is the data after the '+' that is tripping me up, but I haven't been able to find the right solution.
here is an example of one of the data entries, and the line that I am trying to excecute
Example data: "2023-06-09T23:55:00+00:00"
solardates= datetime(solar.Date,'InputFormat','uuuu-MM-dd''T''HH:mm:ss''+','TimeZone','UTC');
And here are the errors I get when running the code.
Error using datetime (line 257)
Unable to convert the text to datetime using the format 'uuuu-MM-dd'T'HH:mm:ss'+'.
Error in HistTest (line 30)
solardates= datetime(solar.Date,'InputFormat','uuuu-MM-dd''T''HH:mm:ss''+','TimeZone','UTC');

回答 (1 件)

Steven Lord
Steven Lord 2025 年 1 月 23 日 18:48
Looking at the description of the Format property on the documentation page for the datetime function I think you want ZZZZZ or perhaps Z in the format. The description of those identifiers in the "Time Zone Offset Formats" table each mention ISO 8601.
solar.Date = "2023-06-09T23:55:00+00:00"
solar = struct with fields:
Date: "2023-06-09T23:55:00+00:00"
I changed firstPartOfFormat from a char vector to a string array, so there's no need to use two single quotes.
firstPartOfFormat = "uuuu-MM-dd'T'HH:mm:ss"
firstPartOfFormat = "uuuu-MM-dd'T'HH:mm:ss"
solardates1 = datetime(solar.Date,'InputFormat', firstPartOfFormat+"Z",'TimeZone','UTC')
solardates1 = datetime
09-Jun-2023 23:55:00
solardates2 = datetime(solar.Date,'InputFormat', firstPartOfFormat+"ZZZZZ",'TimeZone','UTC')
solardates2 = datetime
09-Jun-2023 23:55:00
  1 件のコメント
Klynton
Klynton 2025 年 1 月 27 日 12:36
Thanks for answersing! I realized immediately after posting that I could just reimport the data with a custom delimiter and remove the offending 0's and got it to work that way!

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

カテゴリ

Help Center および File ExchangeTime Series Objects についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by