date functions in R return wrong year

前端 未结 2 1496
谎友^
谎友^ 2021-01-23 07:21

I am trying to convert a character field into a date field for which the options are to either use strptime function or as.Date function. Here is two r

2条回答
  •  既然无缘
    2021-01-23 08:12

    Your date format string should use a capital "Y", which captures the full date with century, such as "2015". Lowercase "y" is used to capture the year in two digits, as when "2015" is written in shorthand as "15". In your original cases, the strptime and as.Date functions incorrectly interpret the "20" in "2015" as the 2-digit year. The corrected version:

    strptime(c("5/13/2015"),"%m/%d/%Y")
    
    "2015-05-13 EDT"
    

提交回复
热议问题