how to extract time difference?


lubridate::with_tz("2023-01-01 00:00:01", tzone = "Pacific/Chatham")

will produce "2023-01-01 16:45:01 +1345"

I would like to extract +1345

Hi @melgoussi! This can be accomplished with the format() function.

mydate = lubridate::with_tz("2023-01-01 00:00:01", tzone = "Pacific/Chatham")

#> [1] "2023-01-01 18:45:01 +1345"

format(mydate, format = '%Z')
#> [1] "+1345"

Created on 2023-08-11 with reprex v2.0.2


may i ask what is %Z since i could not find in formatting Date?

The help for strptime lists %z.


From the documentation @FJCC references:
" Signed offset in hours and minutes from UTC, so -0800 is 8 hours behind UTC. Values up to +1400 are accepted. (Standard only for output. For input R currently supports it on all platforms.)"

1 Like

but i run this code,

lubridate::with_tz("2023-01-01 00:00:01", tzone = "Europe/Andorra")

it will return "CET" not UTC???

By setting tzone to "Europe/Andorra", you are telling with_tz() to return a time in that time zone, which is CET. If you want to return a time in UTC, use ymd_hms().

TheTime <- ymd_hms("2023-01-01 00:00:01", tz = "UTC")
[1] "2023-01-01 00:00:01 UTC"
with_tz(TheTime, tzone = "Europe/Andorra")
[1] "2023-01-01 01:00:01 CET"

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.