FRANCH有日期格式,如何转换为正常格式?

发布于 2025-01-16 20:16:49 字数 152 浏览 2 评论 0原文

FRANCH有日期格式,如何转换为正常格式?我想要类似于 2022-2-7 2022 12:36:10 UTC 的格式

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

There is date format from FRANCH, how to pase to normal format? I want the format like 2022-2-7 2022 12:36:10 UTC

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七禾 2025-01-23 20:16:49

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

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

但是,您的法语区域设置可能会以不同的方式命名(请参阅 这个),除非您以法语安装操作系统,否则您不太可能有首先安装了语言环境。

第二个选项是将月份翻译为您当前的区域设置。对我来说这是英语,所以我可以快速实现这个功能:

# 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 创建于 2022-03-25包 (v2.0.1)

There are two options (that I know of). If you have a French locale installed, you can set it as an option in your lubridate functions:

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

However, your French locale could be named differently (see this) and unless you installed your operating system in French, it is unlikely you have the locale installed in the first place.

The second option is to translate the month into your current locale. For me this is English, so I can whip up this quick function:

# 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"

Created on 2022-03-25 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文