autodate: Universal date converter

autodateR Documentation

Universal date converter

Description

At your own risk: this aims for the most-sensible interpretation of a character vector of dates in whatever godawful format they may be, to avoid the delights of strptime. "Most sensible" is according to me; but you (or the originator of the dataset) might have different ideas, and if so it's your problem. See Details for, you guessed it, DETAILS.

Usage

autodate(datestr, ct=TRUE)

Arguments

datestr

character vector

ct

whether to return POSIXct (the default) or POSIXlt object

Details

All dates in the vector must have the same format as each other. Each must have a Day, Month, and Year, in any order except that Year cannot be in the middle, separated by either "/" or "-". Spaces are ignored. Month can be numbers, 3-letter abbreviation, or full month name. Year can be either 2- or 4-digits, but (unlike strptime itself) all digits are checked; note that strptime will uncomplainingly accept 1/1/2099 as coming from AD20 if you tell it %Y, even tho IMO you should have to write eg 1/1/0020 if you want stuff pre-AD1000, and autodate will enforce that. Consequently, leading zeros on Day and Month are ignored, but are honoured on Year.

In case of ambiguous results (which are common, especially with Day and 2-digit Year), the version with the smallest range is chosen; if several versions have equal range, the most recent (or furthest-future) is chosen.

Value

POSIXct or POSIXlt object, always with timezone GMT. Attributes (dim etc) should be preserved.

Examples

## Should add more...
## Unambigous:
autodate( '1-Mar-2017')
# [1] "2017-03-01 GMT"
## Stupid
autodate( '1/1/1')
# Warning in autodate("1/1/1") :
#   Ambiguous date format: gonna pick futurest...
# [1] "2001-01-01 GMT"
## Ancient: NB 4 digits.
autodate( '1/13/0001')
# [1] "0001-01-13 GMT"
## Lazy, 2-digit year: assume modern
autodate( '1/13/01')
#  Warning in autodate("1/13/01") :
#    Ambiguous date format: gonna pick futurest...
#  [1] "2001-01-13 GMT"
## Corner case...
autodate( character(), ct=FALSE)
# POSIXlt of length 0
autodate( character())
# character(0) # actually CORRECT-- it really is 'POSIXct'-- but just prints as if wrong

mvbutils documentation built on May 25, 2026, 5:09 p.m.

Related to autodate in mvbutils...