How do you change the timezone of Sys.time()

我怕爱的太早我们不能终老 提交于 2021-02-18 22:38:49

问题


I am in the PDT timezone and I want to change the variable "s" to the GMT timezone. Any idea how?

s<-Sys.time()
s
as.POSIXct(s,"GMT")

OUTPUT

> s<-Sys.time()
> s
[1] "2015-06-17 17:56:17 PDT"
> as.POSIXct(s,"GMT")
[1] "2015-06-17 17:56:17 PDT" # <--  how do I get this in GMT??

回答1:


Depending on what you want to do exactly, there are a couple of options:

s <- Sys.time()
s
#[1] "2015-06-18 11:21:22 EST"

Transfer from local time to GMT, without adustment:

as.POSIXct(format(s),tz="GMT")
#[1] "2015-06-18 11:21:22 GMT"

Transfer to GMT, adjusting for the time difference between local time and GMT.

`attr<-`(s,"tzone","GMT")
#[1] "2015-06-18 01:21:22 GMT"

, which is equivalent to the assignment operation:

attr(s,"tzone") <- "GMT"



回答2:


You can also use .POSIXct:

s <- .POSIXct(s, "GMT")


来源:https://stackoverflow.com/questions/30904851/how-do-you-change-the-timezone-of-sys-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!