FRANCH有日期格式,如何粘贴到正常格式?

There is date format from FRANCH, how to pase to normal format?

有来自FRANCH的日期格式,如何粘贴到正常格式?我想要 2022-2-7 2022 12:36:10 UTC

这样的格式
lubridate::dmy_hms("7 févr. 2022 12:35:10 UTC")

有两种选择(据我所知)。如果您安装了法语语言环境,则可以将其设置为 lubridate 函数中的一个选项:

lubridate::dmy_hms("7 févr. 2022 12:35:10 UTC", locale = "fr_FR")

但是,您的法语语言环境可能有不同的命名方式(请参阅 this),除非您以法语安装操作系统,否则您一开始就不太可能安装语言环境。

第二个选项是将月份翻译成您当前的语言环境。对我来说这是英语,所以我可以启动这个快速功能:

# get a dictionary first
months <- c(
  "janvier"   = "January",
  "février"   = "February",
  "mars"      = "March",
  "avril"     = "April",
  "mai"       = "May",
  "juin"      = "June",
  "juillet"   = "July",
  "août"      = "August",
  "septembre" = "September",
  "octobre"   = "October",
  "novembre"  = "November",
  "décembre"  = "December",
  # also include abbreviations
  "janv." = "January",
  "févr." = "February",
  "mars" = "March",
  "avril" = "April",
  "mai" = "May",
  "juin" = "June",
  "juil." = "July",
  "août" = "August",
  "sept." = "September",
  "oct." = "October",
  "nov." = "November",
  "déc." = "December"
)

# define a new function which includes translation
dmy_hms2 <- function(x) {
  x <- stringi::stri_replace_all_fixed(
    x,
    pattern = names(months),
    replacement = months,
    vectorize_all = FALSE
  )
  lubridate::dmy_hms(x)
}
# now it all works
dmy_hms2(x = "7 févr. 2022 12:35:10 UTC")
#> [1] "2022-02-07 12:35:10 UTC"

reprex package (v2.0.1)

于 2022-03-25 创建