【问题标题】:Problem to convert string with month abbreviation to POSIXlt [duplicate]将带有月份缩写的字符串转换为POSIXlt的问题[重复]
【发布时间】:2020-05-25 20:22:12
【问题描述】:

我尝试使用POSIXlt 将 Excel 日期字符转换为日期时间类。这是我的数据示例:"01:56:00 06-Apr-2017"

对于format,我使用了字符串,它给出了strptime 使用的日期时间格式。

我试过as.POSIXlt(new_dtime, format = "%H:%M:%S %d-%b-%Y"),但结果是一堆NA。我确信问题与月份缩写有关,尽管我使用了%b,正如strptime 建议的那样。有什么帮助吗?

【问题讨论】:

  • 根据你的例子,我得到str1 <- "01:56:00 06-Apr-2017"; as.POSIXct(str1, format = "%H:%M:%S %d-%b-%Y") [1] "2017-04-06 01:56:00 EDT"

标签: r datetime posixlt


【解决方案1】:

我猜测这是一个语言环境问题:来自?strptime%b 表示“[a]缩写月份名称在此平台上的当前语言环境中”(强调)。 “Apr”是英语语言环境中 April 的缩写。 This question 建议以下解决方案:

str1 <- "01:56:00 06-Apr-2017" 
orig_locale <- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME", "C")
as.POSIXct(str1, format = "%H:%M:%S %d-%b-%Y")
Sys.setlocale("LC_TIME", orig_locale)

【讨论】:

  • 成功了!非常感谢:)
猜你喜欢
  • 1970-01-01
  • 2014-03-30
  • 2018-08-05
  • 2021-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多