用 r 舍入日期

Rounding a date with r

我有这个日期格式“2019 年 3 月 12 日,15:08:27.174”,我想把它四舍五入,所以至于保持那个小时和分钟。 谢谢

我们可以在将 lubridate 转换为日期时间后使用 ceiling_date

library(lubridate)
ceiling_date(mdy_hms("March 12th 2019, 15: 08: 27.174"), unit = "minutes")
#[1] "2019-03-12 15:09:00 UTC"

或者,如果您只想 round 使用 round_date

round_date(mdy_hms("March 12th 2019, 15: 08: 27.174"), unit = "minutes")
#[1] "2019-03-12 15:08:00 UTC"

也可以用基数 R

round(as.POSIXct("March 12th 2019, 15: 08: 27.174", 
                format = "%B %dth %Y, %H:%M:%S"), "mins")

#[1] "2019-03-12 15:08:00 +08"