How can I change an entire column of dates into datetime format?

15 次查看(过去 30 天)
Hello,
I am trying to convert an entire column of dates into datetime format.
the first cell of the column reads as follows:
'1940/01/11 18:00:00+00'
I have tried the following code;
datetime(Table.ColumnName, 'InputFormat', 'dd-MMM-uuuu HH:mm:ss.SSS')
but keep getting an error and that it is Unable to convert the text to datetime using the
format 'dd-MMM-uuuu HH:mm:ss.SSS'.
Can someone let me know what I am doing wrong and provide a solution?
Thank you!!!
  1 个评论
Stephen23
Stephen23 2023-9-29
编辑:Stephen23 2023-9-29

“Can someone let me know what I am doing wrong and provide a solution?“

Just to make it clear: the “+00“ bit at the end most likely indicates the timezone:

https://en.wikipedia.org/wiki/Time_zone

And not fractional seconds like you specified. The solution is to specify the timezone in the input format (i.e. Z or X or as a literal).

请先登录,再进行评论。

回答(2 个)

Star Strider
Star Strider 2023-9-28
编辑:Star Strider 2023-9-28
It appears to be UCT (or GMT in 1940).
Perhaps this (the 'InputFormat' descriptor must match the format of the string you want to convert) —
Table.ColumnName = '1940/01/11 18:00:00+00';
DT = datetime(Table.ColumnName, 'InputFormat', 'uuuu/MM/dd HH:mm:ss+00', 'Timezone','UCT')
Warning: 'UCT' specifies a time zone with a fixed offset from UTC, +00:00. This zone does not follow daylight saving time, and so may give unexpected results. See the datetime.TimeZone property for details about specifying time zones.
DT = datetime
11-Jan-1940 18:00:00
EDIT — Be sure that this is 11 Jan and not 01 Nov. The day and month fields are ambiguous.
.

Piiotr Botew
Piiotr Botew 2023-9-28
编辑:Piiotr Botew 2023-9-28
You can use this format:
infmt = 'yyyy/MM/dd HH:mm:ss+SSS';
date = '1940/01/11 18:00:00+00';
datetime(date, 'InputFormat', infmt)
ans = datetime
11-Jan-1940 18:00:00
  1 个评论
Walter Roberson
Walter Roberson 2023-9-28
u, uu, ...ISO year. A single number designating the year. An ISO year value assigns positive values to CE years and negative values to BCE years, with 1 BCE being year 0.
Not sure why you say there is no such thing in the documentaiton ?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Time Series Objects 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by