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

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Dates and Time 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by