How to validate date in R

how to test date format in r
change date format in r
is date
r convert column to datetime
r declare date
today's date in r
r variable as date
r date labels

I have a date in the format dd-mm-yyyy HH:mm:ss What is the best and easiest way to validate this date?

I tried

d <- format.Date(date, format="%d-%m-%Y %H:%M:%S")

But how can I catch the error when an illegal date is passed?

Simple way:

d <- try( as.Date( date, format= "%d-%m-%Y %H:%M:%S" ) )
if( class( d ) == "try-error" || d ) ) print( "That wasn't correct!" )

Explanation: format.Date uses as.Date internally to convert date into an object of the Date class. However, it does not use a format option, so as.Date uses the default format, which is %Y-%m-%dand then %Y/%m/%d.

The format option from format.Date is used only for the output, not for the parsing. Quoting from the as.Date man page:

The ‘as.Date’ methods accept character strings, factors, logical ‘NA’ and objects of classes ‘"POSIXlt"’ and ‘"POSIXct"’. (The last is converted to days by ignoring the time after midnight in the representation of the time in specified timezone, default UTC.) Also objects of class ‘"date"’ (from package ‘date’) and ‘"dates"’ (from package ‘chron’). Character strings are processed as far as necessary for the format specified: any trailing characters are ignored.

However, when you directly call as.Date with a format specification, nothing else will be allowed than what fits your format.

See also: ?as.Date

R Language, R Language Validate a date in a "YYYYMMDD" format. Example#. It is a common practice to name files using the date as prefix in the following format:� Examples # NOT RUN { is.Date(as.Date("2009-08-03")) # TRUE is.Date(difftime(now() + 5, now())) # FALSE # } Documentation reproduced from package lubridate, version 1

You may want to look at the gsubfn package. This has functions (gsubfn specifically) that work like other regular expression functions to match pieces to a string, but then it calls a user supplied function and passes the matching pieces to this function. So you would write your own function that looks at the year, moth, and day and makes sure that they are in the correct ranges (and the range for day can depend on the passed month and year.

Testing dates in R, Testing dates in R - an IsDate() function R. # Trying to find a basic test for dates in R. was looking for a is. Check if field is a date using as. The validate package is intended to make checking your data easy, maintainable, and reproducible. The package allows you to. Test your data set(s) against predefined rules, either in- or cross-data set; Import and export rule sets from structured or free-format files; Investigate and visualize the results of a data validation step

I believe that what you are looking for is the tryCatch function. The following as an excerpt from a script I wrote which accepts any .csv file with two series that have a common x axis. The first column in 'data' is the common x axis variable, and columns 2 & 3 are the y axis variables. I needed the tryCatch statement to make sure the script would create a plot regardless of whether the x axis data is a time series, or some other type of variable.

data = read.csv("STLDvsNEM2.csv", header = TRUE)

x <- tryCatch({
  warning = function(w) {},
  error = function(e) {
    x <- data[,1]
y1 <- data[,2]
y2 <- data[,3]

is.Date function, Date() mirrors primitive contructors in base R ( double() , character() etc.) Package ‘validate’ December 16, 2019 Maintainer Mark van der Loo <> License GPL-3 Title Data Validation Infrastructure LazyData no Type Package LazyLoad yes Description Declare data validation rules and data quality indicators; confront data with them and analyze or visualize the results.

This might be helpful if flexibility is desired in a date-time entry.

I have a function where I want to allow either a date-only entry or a date-time entry, then set a flag - for use inside the function only. I'm calling this flag data_type. The flag will be used later in the larger function to select units for getting a difference in two dates with difftime. (In most cases, the function will be perfectly fine with date only, but in some cases a user might need a shorter time frame. I don't want to inconvenience users with the shorter time frame if they don't need it.)

I am posting this for two reasons: 1) to help anyone trying to allow flexibility in date arguments and 2) to welcome sanity checks in case there's a problem with the method, since this is going into a function in an R package.

dat_time_check_fn <- function(dat_time) {
  if (!anyNA(as.Date(dat_time, format= "%Y-%m-%d %H:%M:%S"))) date_type <- 1
else if (!anyNA(as.Date(dat_time, format= "%Y-%m-%d"))) date_type <- 2
else stop("Error: dates must either be in format '1999-12-31' or '1999-12-31 23:59:59' ")

Date-time case

date5 <- "1999-12-31 23:59:59"

date_type <- dat_time_check_fn(date5)
[1] 1

Date only case:

date6 <- "1999-12-31"

date_type <- dat_time_check_fn(date6)
[1] 2

Note that if the order above in the function is reversed, the longer date-time can be inadvertently coerced to the shorter version and both types result in date_type = 1.

My larger function has more than one date, but I need them to be compatible. Below, I'm checking the two dates checked above, where one was type 1 and one was type 2. Combining types gives the result with date only (type 2):

date_type <- dat_time_check_fn(c(date5, date6))
[1] 2

Here's a non-compliant version:

date7 <- "1/31/2011"

date_type <- dat_time_check_fn(date7)

Error in dat_time_check_fn(date7) : 
  Error: dates must either be in format '1999-12-31' or '1999-12-31 23:59:59'

IsDate function, Check if the given x is of any known Date type. This checks for many known Date and Time classes: "POSIXt", "POSIXct", "dates", "times", "chron", "Date". U nder the theory section, in the Model Validation section, two kinds of validation techniques were discussed: Holdout Cross Validation and K-Fold Cross-Validation.. In this blog, we will be studying the application of the various types of validation techniques using R for the Supervised Learning models.

[PDF] anytime: Easier Date and Time Conversion, R excels at computing with dates, and times. Using a package=anytime assertTime() and assertDate() functions validate if the given. How to validate date format in shell script. There are multiple ways you can validate date format in shell script. You can compare date format with regular expression or you can use inbuilt date command to check given date format is valid or not. I am sharing a simple date command to validate the date in YYYY-mm-dd format. #!/usr/bin/bash d="2019-12-01"

Dates and Times in R, R provides several options for dealing with date and date/time data. The builtin as.Date function handles dates (without times); the contributed library chron� Data Validation Infrastructure. Declare data validation rules and data quality indicators; confront data with them and analyze or visualize the results. The package supports rules that are per-field, in-record, cross-record or cross-dataset. Rules can be automatically analyzed for rule type and connectivity.

JavaScript to Validate Date Entry, How to validate Date (calendar) controls in a guided ad-hoc HTML page? There are so many problems with users not entering the date in mm/dd/yyyy,… If you have questions about this article or would like to discuss ideas presented here, please post on RStudio Community.Our developers monitor these forums and answer questions periodically.

  • getting "That wasn't correct!" for input "01-13-1986 00:00:00" but No error on "39-12-1986 00:00:00" do how can I ensure the validity day for a date ??
  • That's correct but how can I check the validity of day as no message for date having day greater than 31??