简体   繁体   English

在 R 中,取一个日期字符串并将其转换为日期时间格式(使用 lubridate)

[英]In R, take a date string and convert it to datetime format (using lubridate)

In RI am trying to take a date string and convert it to date time format using lubridate but anm getting an error that:在 RI 中,我尝试获取日期字符串并使用 lubridate 将其转换为日期时间格式,但出现以下错误:

All formats failed to parse. No formats found. 

Using this code:使用此代码:

lubridate::as_date("1/2/34")

Shouldn't this just return a formated date time?这不应该只返回格式化的日期时间吗?

as.Date or as_Date needs format . as.Dateas_Date需要format By default, it can parse if the format is %Y-%m-%d .默认情况下,它可以解析format是否为%Y-%m-%d Here, it is not the case.在这里,情况并非如此。 So所以

lubridate::as_date("1/2/34", format ="%d/%m/%y")

Or more compactly或更紧凑

lubridate::dmy("1/2/34")

Based on the string, it is not clear whether it is day/month/year or month/day/year.根据字符串,不清楚是日/月/年还是月/日/年。 Also, for 2-digit year, there is an issue with prefix ie it can be either "19" or "20".此外,对于 2 位数年份,前缀存在问题,即它可以是“19”或“20”。 Here, it would parse at "2034"在这里,它将解析为“2034”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM