Extracting 2 digit hour from POSIXct in R

夙愿已清 提交于 2019-12-30 11:04:09

问题


I would like to extract the hour from a POSIXct time in R, but retrieve the 2 digit answer.

For example,

test=as.POSIXct("2015-03-02 03:15:00")
test
[1] "2015-01-02 03:15:00 GMT"
month(testing)
[1] 1
hour(testing)
[1] 3

The results give the relevant month and hour, but I would like to see 01 and 03 instead of just 1 and 3.


回答1:


Try to do this:

strftime(test, format="%H")

to extract hours and

strftime(test, format="%m")

for month



来源:https://stackoverflow.com/questions/35227648/extracting-2-digit-hour-from-posixct-in-r

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