【问题标题】:Date format change UTC [duplicate]日期格式更改 UTC [重复]
【发布时间】:2025-12-25 23:20:25
【问题描述】:

如何在 Rstudio 中将此日期格式 2020/02/06T08:14:26z 更改为“更简单的日期”?

注意“z”是一个小写字母

【问题讨论】:

    标签: r datetime-format


    【解决方案1】:
    df$ddate <- format(as.Date(df$ddate), "%d/%m/%Y")
    
    

    【讨论】:

      【解决方案2】:

      另一个选择是 chron 包 - 它很好,简单,最重要的是,它可以完成这项工作。

      如果您没有安装 chron,请执行以下操作:

       install.packages("chron")
       # load it
       library(chron)
       # make dummy data
       bdate <- c("09/09/09", "12/05/10", "23/2/09")
       wdate <- c("12/10/09", "05/01/07", "19/7/07")
       ddate <- c("2009-09-27", "2007-05-18", "2009-09-02")
       # notice the last argument, it will not allow creation of factors!
       dtf <- data.frame(id = 1:3, bdate, wdate, ddate, stringsAsFactors = FALSE)
       # since we have characters, we can do:
       foo <- transform(dtf, bdate = chron(bdate, format = "d/m/Y"), wdate = chron(wdate, format = "d/m/Y"), ddate = chron(ddate, format = "y-m-d"))
       # check the classes
       sapply(foo, class)
       # $id
       # [1] "integer"
      
       # $bdate
       # [1] "dates" "times"
      
       # $wdate
       # [1] "dates" "times"
      
       # $ddate
       # [1] "dates" "times"
      

      【讨论】: