Unable to read datetime with format "0600 UTC OCT 25"

4 ビュー (過去 30 日間)
dormant
dormant 2023 年 10 月 25 日
編集済み: Star Strider 2023 年 10 月 25 日
When I import data from a kml file using readtable, it stores the date and time in a string array called Name formatted like this:
"1200 UTC OCT 10"
"1800 UTC OCT 10"
"0000 UTC OCT 11"
"0600 UTC OCT 11"
In the latest round of my bout with datetime, I'm struggling to get this into a datetime variable.
I have tried this, but it doesn't work:
datimStorm = datetime( Name, 'InputFormat', 'hhmm ''UTC'' MM yy' );
Can this be done using the 'InputFormat', or do I have to do it myself with substrings?
  1 件のコメント
dormant
dormant 2023 年 10 月 25 日
Thanks to both of you. I made a mistake in cutting and pasting and included 'yy' where I was using 'dd'.
I got confused as well by the difference between "HH" and "hh", but the MATLAB documentation sorted me out.
The format does not include a year. How foolish is that?

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

採用された回答

Star Strider
Star Strider 2023 年 10 月 25 日
That is almost correct. Use 'MMM' for months in that format, and 'HH for hours.
Try this —
Name = ["1200 UTC OCT 10"
"1800 UTC OCT 10"
"0000 UTC OCT 11"
"0600 UTC OCT 11"];
datimStorm = datetime( Name, 'InputFormat', 'HHmm ''UTC'' MMM yy' )
datimStorm = 4×1 datetime array
01-Oct-2010 12:00:00 01-Oct-2010 18:00:00 01-Oct-2011 00:00:00 01-Oct-2011 06:00:00
.
  4 件のコメント
Steven Lord
Steven Lord 2023 年 10 月 25 日
@Jon For a definitive answer to your last question you'd have to ask the authors and reviewers of the Unicode Technical Standard (linked in the documentation for the Format property on the datetime array) whose symbols we (mostly) use. The table in the standard does list a Y symbol as an option for specifying years, but looking at its description I'm not sure how useful that would be for MATLAB which is why I'd guess we didn't implement it.
Star Strider
Star Strider 2023 年 10 月 25 日
編集済み: Star Strider 2023 年 10 月 25 日
Two-digit years are certainly acceptable, and were the norm for a while, due to early computer memory and storage limitations. (This was the problem with the ‘Y2K’ concerns, in that years were stored as two digits for decades. Had the software not been updated and records not been corrected to four-digit years prior to the year 2000, the years would have jumped ahead by a century — 1999 would have become 2099 overnight — causing massive calculation errors.)
I was following the provided 'InputFormat' string, with appropriate changes to be certain that the months and hours were imported correctly, since that appeared to hsve been the issue.
EDIT — (25 Oct 2023 at 17:30)
One relatively important feature that I inadvertently omitted was to declare the 'TimeZone' as 'UTC' since that information was provided (also changing ‘yy’ to ‘dd’) —
Name = ["1200 UTC OCT 10"
"1800 UTC OCT 10"
"0000 UTC OCT 11"
"0600 UTC OCT 11"];
datimStorm = datetime( Name, 'InputFormat', 'HHmm ''UTC'' MMM dd', 'TimeZone','UTC' )
datimStorm = 4×1 datetime array
10-Oct-2023 12:00:00 10-Oct-2023 18:00:00 11-Oct-2023 00:00:00 11-Oct-2023 06:00:00
LocalTime = datimStorm;
LocalTime.TimeZone = 'America/Denver'
LocalTime = 4×1 datetime array
10-Oct-2023 06:00:00 10-Oct-2023 12:00:00 10-Oct-2023 18:00:00 11-Oct-2023 00:00:00
You can then change the 'TimeZone' in a copied array (with a different name) as local time. The initial array will remain defined as UTC.
.

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

その他の回答 (1 件)

Jon
Jon 2023 年 10 月 25 日
t = "1200 UTC OCT 10"
t = "1200 UTC OCT 10"
td = datetime(t,'InputFormat','hhmm ''UTC'' MMM dd')
td = datetime
10-Oct-2023
  2 件のコメント
Jon
Jon 2023 年 10 月 25 日
Ooops should have been
t = "1200 UTC OCT 10"
t = "1200 UTC OCT 10"
td = datetime(t,'InputFormat','HHmm ''UTC'' MMM dd')
td = datetime
10-Oct-2023 12:00:00
dormant
dormant 2023 年 10 月 25 日
Thanks, I had got confused by HH and hh. The format doesn't include a year, so dd is appropriate.

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

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by