Skip to contents

Useful functions to check if a vector represent dates object

Usage

check_date_year(x)

check_date_quarter(x)

check_date_month(x)

check_format_date(x, date_format = "%Y-%m-%d")

Arguments

x

a vector of Date, character, integer or POSIXt object representing date object

date_format

character string (or vector of string) corresponding to the format(s) used in x

Value

a boolean

Details

The function check_date_year checks if the pattern AAAA is recognised. If so, the date will be assimilated with the first January of each year AAAA. The function check_date_quarter checks if the quarterly formats. The accepted formats (for example for the third quarter of 2000) are:

  • 2000 T3

  • 2000 Q3

  • 2000t3

  • 2000q3

  • 2000T3

  • 2000Q3

  • 2000 t3

  • 2000 q3

If one of the previous formats is recognised, the date will be assimilated with the first day of the quarter of the year (For example 2000 Q3 is assimilated to 2000-07-01).

The function check_date_month checks if the monthly formats. The accepted formats (for example for march of 2000) are:

  • 2000 M3

  • 2000 M03

  • 2000 m3

  • 2000 m03

  • 2000M3

  • 2000M03

  • 2000m3

  • 2000m03

If one of the previous formats is recognised, the date will be assimilated with the first day of the month of the year (For example 2000 M3 is assimilated to 2000-03-01).

The function check_format_date checks if the object x match the pattern (or one of the patterns) date_format.

Examples

if (FALSE) { # rjd3jars::check_java_version(silent = TRUE)

# check_date_year --------------------------------------------------

# Good date (representing years)
check_date_year(x = c("2000", "2001", "2002", "2003"))
check_date_year(x = 2020:2024)

# Bad date
check_date_year(x = "2000 ")
check_date_year(x = 1:4)

# check_date_quarter -----------------------------------------------

# Good date
check_date_quarter(x = c("2000 q2", "2000 q3", "2000 q4", "2001 q1"))
check_date_quarter(x = c("2010T1", "2010T2", "2010T3", "2010T4"))
check_date_quarter(x = c("2020Q1", "2020Q2", "2020Q3", "2020Q4"))
check_date_quarter(x = c("2020Q01", "2020Q02", "2020Q03", "2020Q04"))

# Bad date
check_date_quarter(x = "2000 ")
check_date_quarter(x = 1:4)
check_date_quarter(x = "2000 q 2")
check_date_quarter(x = "2000 q12")

# check_date_month -----------------------------------------------

# Good date (representing years)
check_date_month(x = c("2000 m2", "2000 m3", "2000 m4", "2000 m5"))
check_date_month(x = c("2010M9", "2010M10", "2010M11", "2010M12"))
check_date_month(x = c("2020M111", "2020M12", "2021M01", "2021M02"))
check_date_month(x = c("2020M01", "2020M02", "2020M03", "2020M04"))

# Bad date
check_date_month(x = "2000 ")
check_date_month(x = 1:4)
check_date_month(x = "2000 m 2")
check_date_month(x = "2000 m13")

# check_format_date -----------------------------------------------

# Good date (representing years)
check_format_date(x = c("2000-01-01", "2000-02-01", "2000-03-01", "2000-04-01",
                        "2000-05-01", "2000-06-01", "2000-07-01", "2000-08-01",
                        "2000-09-01", "2000-10-01"),
                 date_format = "%Y-%m-%d")
check_format_date(x = c("01/08/2010", "01/09/2010", "01/10/2010", "01/11/2010",
                        "01/12/2010", "01/01/2011", "01/02/2011", "01/03/2011",
                        "01/04/2011", "01/05/2011"),
                 date_format = "%d/%m/%Y")
check_format_date(x = c("2000-01-01", "2000-02-01", "2000-03-01", "2000-04-01",
                        "2000-05-01", "2000-06-01", "2000-07-01", "2000-08-01",
                        "2000-09-01", "2000-10-01"),
                 date_format = c("%Y-%m-%d", "%d/%m/%Y"))

# Bad date
check_format_date(x = c("2000-01-01", "2000-02-01", "2000-03-01", "2000-04-01",
                        "2000-05-01", "2000-06-01", "2000-07-01", "2000-08-01",
                        "2000-09-01", "2000-10-01"),
                 date_format = "%d/%m/%Y")
check_format_date(x = c("01/08/2010", "01/09/2010", "01/10/2010", "01/11/2010",
                        "01/12/2010", "01/01/2011", "01/02/2011", "01/03/2011",
                        "01/04/2011", "01/05/2011"),
                 date_format = "%Y-%m-%d")
}