更新时间:2023-01-31 16:14:42
首先将两个日期都转换为 POSIXct
类。确保在同一时区进行计算,POSIXt类默认为您的语言环境时区, as.Date
默认为UTC。
Convert both dates to the POSIXct
class first. Be sure to do the calculations in the same timezone, the POSIXt classes default to your locale timezone, as.Date
defaults to UTC.
test1 <- as.Date("2016-01-01", tz="UTC")
test2 <- strptime("2016-01-02", format="%Y-%m-%d", tz="UTC")
difftime(as.POSIXct(test2), as.POSIXct(test1, tz="UTC"), units="days")
# Time difference of 1 days