Extracting 2 digit hour from POSIXct in R

2019-02-28 15:15发布

问题:

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