Round a POSIX date and time (posixct) to a date relative to a timezone

冷暖自知 提交于 2019-11-29 14:42:54

round will round to the next day once it's past midday, which is why I think you are seeing 2013-03-06. I also have to explicitly set the tz argument in the call to as.POSIXct

Observe:

round( as.POSIXct("2013-03-05 11:00:00" , tz = "EST" ), "day" )
[1] "2013-03-05 EST"

And then once it passes noon:

round( as.POSIXct("2013-03-05 12:00:00" , tz = "EST" ), "day" )
[1] "2013-03-06 EST"

A call to format extracts the day as a character string without the tz argument. So you can get your original result without the timezone

format( round( as.POSIXct("2013-03-05 12:00:00" , tz = "EST" ), "day" ) )
[1] "2013-03-06"

If you want to round any time on that day to that day perhaps what you want instead is trunc?

format(trunc( as.POSIXct("2013-03-05 12:00:00" , tz = "EST" ), "day" ))
[1] "2013-03-05"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!