Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - Date format check

I am trying to check whether a given date is in dd/mm/yyyy format or not in R language and also whether it is a valid date or not at the same time. I want output in TRUE or FALSE format, e.g.

Input:

date<- c('12/05/2016','35/11/2067','12/52/1000')

Output:

TRUE FALSE FALSE
like image 852
girijesh96 Avatar asked Dec 24 '22 10:12

girijesh96


1 Answers

You can use this function:

IsDate <- function(mydate, date.format = "%d/%m/%y") {
  tryCatch(!is.na(as.Date(mydate, date.format)),  
           error = function(err) {FALSE})  
}

IsDate(date)
[1]  TRUE FALSE FALSE

Original source of the code here.

.

like image 142
Terru_theTerror Avatar answered Jan 14 '23 02:01

Terru_theTerror