如何将 y%m%d%H 格式转换为“%Y%m%d %H:%M:%S"在时间序列数据中 [英] How to convert y%m%d%H format into "%Y%m%d %H:%M:%S" in time series data
问题描述
如何将 y%m%d%H
格式转换为 "%Y%m%d %H:%M:%S"
.我的日期从 1970 年到 2010 年.
How do I convert the y%m%d%H
format into "%Y%m%d %H:%M:%S"
. My dates run from 1970 to 2010.
推荐答案
部分来自评论(如果您可以相应地修改问题就更好了),似乎这不是格式化的情况(%y
vs %Y
或间距/分隔符),但 strptime
/POSIX*t
自动设置格式以跳过指定午夜"时间时的小时/分钟/秒.(这是我目前基于以下示例的猜测,但我可能漏掉了一些东西.)
Going partly from the comments (it would be nice if you could modify the question accordingly), it seems that this is not a case of formatting (%y
vs %Y
or spacing/delimiters), but of strptime
/POSIX*t
automatically setting the format to skip the hours/minutes/seconds when a 'midnight' time is specified. (This is my current guess based on the following examples, but I could have missed something.)
使用 %y
,非午夜时间:
> str(strptime("00020304",format="%y%m%d%H"))
POSIXlt[1:1], format: "2000-02-03 04:00:00"
同上,午夜时间:
> str(strptime("00020300",format="%y%m%d%H"))
POSIXlt[1:1], format: "2000-02-03"
午夜时间(带空格)
> str(strptime("00 02 03 00",format="%y %m %d %H"))
POSIXlt[1:1], format: "2000-02-03"
一个包含一个午夜和一个非午夜时间的向量:
a vector with one midnight and one non-midnight time:
> str(strptime(c("00020300","00020304"),format="%y%m%d%H"))
POSIXlt[1:2], format: "2000-02-03 00:00:00" "2000-02-03 04:00:00"
所以看起来德克的答案是正确的.
So it looks like Dirk's answer is the way to go.
这篇关于如何将 y%m%d%H 格式转换为“%Y%m%d %H:%M:%S"在时间序列数据中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!