How do you convert dates/times from one time zone to another in R?



If I have a date like this in London time: "2009-06-03 19:30", how can I convert it to the equivalent time in the US West Coast?

9/8/2009 5:07:07 PM

Accepted Answer

First, convert the London time to a POSIXct object:

pb.txt <- "2009-06-03 19:30" <- as.POSIXct(pb.txt, tz="Europe/London")

Then use format to print the date in another time zone:

> format(, tz="America/Los_Angeles",usetz=TRUE)
[1] "2009-06-03 11:30:00 PDT"

There are some tricks to finding the right time zone identifier to use. More details in this post at the Revolutions blog: Converting time zones in R: tips, tricks and pitfalls

4/4/2017 7:46:59 PM

Change the tzone attribute of a 'POSIXct' object:

> pb.txt <- "2009-06-03 19:30"  
> <- as.POSIXct(pb.txt, tz="Europe/London")  
> attributes($tzone <- "America/Los_Angeles"  
[1] "2009-06-03 11:30:00 PDT"

Note that this is still a POSIXct object, tzone has changed, and correct offset has been applied:

> attributes(
[1] "POSIXct" "POSIXt" 

[1] "America/Los_Angeles"

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow