将所有日期时间更改为下午 2 点并保持日期不变
change all datetimes to 2PM and keep the dates same
我有日期时间对象,我想将所有时间更改为下午 2 点并保持日期不变。
我用floor_date得到相应日期的开始,然后加上14小时的时间得到下午2点。
有时,结果只显示日期而没有时间。有时它会同时显示日期和时间。
是否有另一种方法可以做到这一点
library(lubridate)
t1 <- floor_date(Sys.time(), unit = "day") + hours(14)
t2 <- floor_date(ymd_hms("2021-08-25 10:36:00"), unit = "day") + hours(14)
您可以将时间部分替换为小时。这是执行此操作的函数。
change_time_to_x <- function(time, x) {
as.POSIXct(sub('\s.*', x, time), tz = 'UTC')
}
input <- lubridate::ymd_hms(Sys.time(), "2021-08-25 10:36:00", "2012-12-31 00:00:00")
change_time_to_x(input, '14:00:00')
#[1] "2021-08-26 14:00:00 UTC" "2021-08-25 14:00:00 UTC" "2012-12-31 14:00:00 UTC"
我有日期时间对象,我想将所有时间更改为下午 2 点并保持日期不变。
我用floor_date得到相应日期的开始,然后加上14小时的时间得到下午2点。
有时,结果只显示日期而没有时间。有时它会同时显示日期和时间。
是否有另一种方法可以做到这一点
library(lubridate)
t1 <- floor_date(Sys.time(), unit = "day") + hours(14)
t2 <- floor_date(ymd_hms("2021-08-25 10:36:00"), unit = "day") + hours(14)
您可以将时间部分替换为小时。这是执行此操作的函数。
change_time_to_x <- function(time, x) {
as.POSIXct(sub('\s.*', x, time), tz = 'UTC')
}
input <- lubridate::ymd_hms(Sys.time(), "2021-08-25 10:36:00", "2012-12-31 00:00:00")
change_time_to_x(input, '14:00:00')
#[1] "2021-08-26 14:00:00 UTC" "2021-08-25 14:00:00 UTC" "2012-12-31 14:00:00 UTC"