Parse dates in format dmy together with dmY using parse_date_time

后端 未结 2 1507

I have a vector of character representation of dates, where formats mostly are dmY (e.g. 27-09-2013), dmy (e.g. 27-09-13), and occasionally some

2条回答
  •  -上瘾入骨i
    2021-01-18 05:08

    This is actually intentional. I recall it now. It is assumed that if you have dates of the form 01-02-1845 and 01-02-03 in the same vector, then it is probably 01-02-0003 what is meant. It also avoids confusion with dates from different centuries. You cannot know if 17-05-13 refers to 20th or 21st century.

    There might have also been a technical reason for this decision, but I don't remember right now.

    .select_formats argument is the way to go:

    my_select <-   function(trained){
      n_fmts <- nchar(gsub("[^%]", "", names(trained))) +
        grepl("%y", names(trained))*1.5
      names(trained[ which.max(n_fmts) ])
    }
    
    parse_date_time(c("27-09-13", "27-09-2013"), "dmy", select_formats = my_select)
    ## [1] "2013-09-27 UTC" "2013-09-27 UTC"
    

    select_formats should return formats to be applied sequentially to the input character vector. In the above example you give precedence to %y format.

    I am adding this example to the docs.

提交回复
热议问题