Parse ISO 8601 combined date and time YYYY-MM-DDTHH-MM-SSZ

倾然丶 夕夏残阳落幕 提交于 2020-07-03 13:04:09

问题


I have a large dataframe with time stamps that look like this:

"2019-05-15T01:42:15.072Z"

It resembles a ISO 8601 combined date and time representation.

How can I parse this string into a real date-time format?

The characters (T and Z) inside the data seems to make it difficult.


回答1:


You can simply parse the timestamp by specifying the format in as.POSIXct (or strptime)

as.POSIXct("2019-05-15T01:42:15.072Z", format = "%Y-%m-%dT%H:%M:%OSZ")
#[1] "2019-05-15 01:42:15 AEST"

Explanation:

%Y, %m and %d denote the year (with century), month and day; %H, %M and %OS denote the hours, minutes and seconds (including milliseconds). The T and Z are simply added to the format string, because

Any character in the format string not part of a conversion specification is interpreted literally

See ?strptime for the different conversion specifications.



来源:https://stackoverflow.com/questions/57355948/parse-iso-8601-combined-date-and-time-yyyy-mm-ddthh-mm-ssz

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!