date functions in R return wrong year

前端 未结 2 1473
谎友^
谎友^ 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:10

    I would highly recommend lubridate for date conversions

    library(lubridate)
    
    mdy("5/13/2015")
    
    # [1] "2015-05-13"
    
    0 讨论(0)
  • 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"
    
    0 讨论(0)
提交回复
热议问题