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

后端 未结 4 2193
南旧
南旧 2020-11-27 13:27

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?

4条回答
  •  没有蜡笔的小新
    2020-11-27 13:55

    If you'd like to do this in one line, recall that any POSIXct object in R is really just a number (seconds UTC since epoch beginning), and that the "timezone" is just an attribute that determines how that number is printed.

    Therefore, we can use the .POSIXct helper function as follows:

    x = as.POSIXct("2009-06-03 19:30", tz = "Europe/London")
    .POSIXct(as.integer(x), tz = 'America/Los_Angeles')
    # [1] "2009-06-03 11:30:00 PDT"
    

    as.integer strips the class and attributes of x, and .POSIXct is shorthand for constructing a POSIXct object; if your object has milliseconds and you'd like to keep track of them, you can use as.numeric(x).

提交回复
热议问题