Learn R Programming

mStats (version 3.2.2)

formatDate: Format Dates

Description

formatDate converts characters or numbers to dates. is.Date indicates which elements are Dates.

Usage

formatDate(x, format = "dmY", sep = "/", century = NULL)

is.Date(x)

year(x)

month(x)

day(x)

Arguments

x

a character or numeric object

format

only for character vectors:

sep

separator character for date components

century

specify either 2000 or 1900 for two-digit years

Details

dmy represents dd mm YYYY format. In combination with separators from sep, this can change to several date formats. For example, dmy + - convert to dd-mm-yyyy format.

Possible conversions

  1. dmy + - >>> dd-mm-yyyy

  2. mdy + - >>> mm-dd-yyyy

  3. ymd + - >>> yyyy-mm-dd

  4. dmy + / >>> dd/mm/yyyy

  5. mdy + / >>> mm/dd/yyyy

  6. ymd + / >>> yyyy/mm/dd

Numeric conversions

Origin is set at 1899-12-30.

See as.Date for more date formats.

Examples

Run this code
# NOT RUN {

## convert strings to dates
x <- c("2019-01-15", "2019-01-20", "2019-01-21", "2019-01-22")

# check if it is a Date format
is.Date(x)

y <- formatDate(x, "Ymd", "-")

# check if it is a Date format
is.Date(y)
y



## convert numbers to dates
x <- 42705:42710
y <- formatDate(x)
is.Date(y)
y


## get day, month or year
day(y)
month(y)
year(y)

# }

Run the code above in your browser using DataLab