【问题标题】:Use strptime to convert time zone in R使用 strptime 在 R 中转换时区
【发布时间】:2021-01-02 15:08:21
【问题描述】:

我的“日期/时间”字段的示例是:

2020-12-30T03:32:30.000Z

我希望将此时区转换为“tz = 新加坡”。 当我执行这段代码时,

geodf$time <- strptime(geodf$time, format = "%Y-%m-%dT%H:%M:%OS", tz="Singapore")

结果输出是:

“2020-12-30 03:32:30 +08”

它所做的唯一更改是背面的附加标签 (+08)。时间完全没有改变 - 我需要将时间更改为新加坡的,而不仅仅是添加 +08。

出了什么问题?

【问题讨论】:

    标签: r timezone strptime


    【解决方案1】:

    您的时间中的Z 表示时区是UTC。一种方法是使用 POSIXct 类的对象并更改时区:

    geodf$time <- as.POSIXct(geodf$time,
                             format = "%Y-%m-%dT%H:%M:%OS", tz="UTC")
    geodf
    #                 time
    #1 2020-12-30 03:32:30
    
    attributes(geodf$time)$tzone <- "Singapore"
    geodf
    #                 time
    #1 2020-12-30 11:32:30
    

    样本数据:

    geodf <- structure(list(time = "2020-12-30T03:32:30.000Z"),
                       class = "data.frame", row.names = c(NA, -1L))
    

    【讨论】:

    • 非常感谢。这完美地解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2016-10-07
    相关资源
    最近更新 更多