Question Details

No question body available.

Tags

r datetime-conversion

Answers (3)

March 11, 2026 Score: 6 Rep: 106,132 Quality: High Completeness: 60%

Option 1

You can use as.POSIXct(Sys.Date()) to retrieve the default 00:00:00 local time, and then apply the offset (Hours 60 minutes 60 seconds)

format(as.POSIXct(Sys.Date()) + hours3600, "%H:%M")

and you will see

[1] "22:00" "22:30" "00:00" "02:30" "04:00"

If you don't mind dates and also keep the Date format, you can simply run

as.POSIXct(Sys.Date()) + hours
3600

which gives

[1] "2026-03-10 22:00:00 UTC" "2026-03-10 22:30:00 UTC" [3] "2026-03-11 00:00:00 UTC" "2026-03-11 02:30:00 UTC" [5] "2026-03-11 04:00:00 UTC"

Option 2

If you want to skip using as.POSIXCT + Sys.Date but manipulate hour offsets directly, you can use

x
March 11, 2026 Score: 4 Rep: 21,870 Quality: Medium Completeness: 50%

You can coerce hms, which is basically a difftime, to POSIXct to get date/times in relation to 1970-01-01 00:00:00 and format as needed to get a character vector:

hours  
  as.POSIXct() |> 
  format("%H:%M")
#> [1] "22:00" "22:30" "00:00" "02:30" "04:00"

Or convert date/times back to hms if it's more suitable for your next step:

( hms as.POSIXct() |> hms::ashms() ) #> 22:00:00 #> 22:30:00 #> 00:00:00 #> 02:30:00 #> 04:00:00 dput(hms_) #> structure(c(79200, 81000, 0, 9000, 14400), units = "secs", class = c("hms", "difftime"))
March 11, 2026 Score: 3 Rep: 78,357 Quality: Medium Completeness: 70%

Something like this?

add_hours