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

r

Question

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?

1
32
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"
pb.date <- as.POSIXct(pb.txt, tz="Europe/London")

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

> format(pb.date, 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

16
4/4/2017 7:46:59 PM

Change the tzone attribute of a 'POSIXct' object:

> pb.txt <- "2009-06-03 19:30"  
> pb.date <- as.POSIXct(pb.txt, tz="Europe/London")  
> attributes(pb.date)$tzone <- "America/Los_Angeles"  
> pb.date  
[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(pb.date)
$class
[1] "POSIXct" "POSIXt" 

$tzone
[1] "America/Los_Angeles"

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