在西班牙语语言环境中获取as.Date的月份格式为缩写的NA [英] Getting an NA with abbreviated month format for as.Date in Spanish locale

查看:84
本文介绍了在西班牙语语言环境中获取as.Date的月份格式为缩写的NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的会议用西班牙语进行.当我使用全月名称(%B )格式时,我得到正确的日期:

My session is in Spanish. When I have a format with full month name (%B) I get the correct date:

as.Date("01-Febrero-2021", format = "%d-%B-%Y")
# [1] "2021-02-01"

但是,当我尝试使用缩写的月份(%b )时,会得到一个"NA":

However, when I try to use the abbreviated month (%b), I get an "NA":

as.Date("01-Feb-2021", format = "%d-%b-%Y")
# [1] NA

as.Date("01-feb-2021", format = "%d-%b-%Y")
# [1] NA

as.Date("01-FEB-2021", format = "%d-%b-%Y")
# [1] NA

我做错了什么?

感谢 G. Grothendieck 的回答,我可以让它发挥作用:

Thanks to the answer from G. Grothendieck, I could make it work:

Sys.getlocale("LC_TIME")
# [1] "Spanish_Argentina.1252"

Sys.Date上使用format检查缩写的月份时,发现月份是用句点写的:

When checking the abbreviated month using format on Sys.Date, it turns out that the month is written with a period:

format(Sys.Date(), "%b")
# [1] "feb."

尝试使用时间段来解析字符串:

Try to parse a string, this time with a period:

as.Date("01-feb.-2021", format = "%d-%b-%Y")
# [1] "2021-02-01"

有效!

推荐答案

它对我有用.确保您实际上在西班牙语言环境中.(我在Windows上,并且在其他平台上可能有不同的工作方式.)

It works for me. Make sure you are actually in a Spanish locale. (I am on Windows and it is possible that it works differently on other platforms.)

Sys.setlocale("LC_TIME", "English")
## [1] "English_United States.1252"
Sys.getlocale("LC_TIME")
## [1] "English_United States.1252"

format(Sys.Date(), "%b")
## [1] "Feb"

现在改为西班牙语:

Sys.setlocale("LC_TIME", "Spanish")
## [1] "Spanish_Spain.1252"
Sys.getlocale("LC_TIME")
## [1] "Spanish_Spain.1252"

format(Sys.Date(), "%b")
## [1] "feb"

as.Date("01-feb-2021", format="%d-%b-%Y")
## [1] "2021-02-01"
as.Date("01-Feb-2021", format="%d-%b-%Y")
## [1] "2021-02-01"

这篇关于在西班牙语语言环境中获取as.Date的月份格式为缩写的NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆