Skip to contents

Convert from Excel date number to R Date type

Usage

convert_date(x, origin = "1900-01-01", ...)

convert_datetime(x, origin = "1900-01-01", ...)

convert_hms(x)

Arguments

x

A vector of integers

origin

date. Default value is for Windows Excel 2010

...

Arguments passed on to base::as.Date.character

format

character string. If not specified, it will try tryFormats one by one on the first non-NA element, and give an error if none works. Otherwise, the processing is via strptime() whose help page describes available conversion specifications.

tryFormats

character vector of format strings to try if format is not specified.

optional

logical indicating to return NA (instead of signalling an error) if the format guessing does not succeed.

Value

A date, datetime, or hms.

Details

Excel stores dates as number of days from some origin day

See also

Examples

# date --
## 2014 April 21st to 25th
convert_date(c(41750, 41751, 41752, 41753, 41754, NA))
#> [1] "2014-04-21" "2014-04-22" "2014-04-23" "2014-04-24" "2014-04-25"
#> [6] NA          
convert_date(c(41750.2, 41751.99, NA, 41753))
#> [1] "2014-04-21" "2014-04-22" NA           "2014-04-24"

# datetime --
##  2014-07-01, 2014-06-30, 2014-06-29
x <- c(41821.8127314815, 41820.8127314815, NA, 41819, NaN)
convert_datetime(x)
#> [1] "2014-07-01 19:30:20 UTC" "2014-06-30 19:30:20 UTC"
#> [3] NA                        "2014-06-29 00:00:00 UTC"
#> [5] NA                       
convert_datetime(x, tz = "Australia/Perth")
#> [1] "2014-07-01 11:30:20 UTC" "2014-06-30 11:30:20 UTC"
#> [3] NA                        "2014-06-28 16:00:00 UTC"
#> [5] NA                       
convert_datetime(x, tz = "UTC")
#> [1] "2014-07-01 19:30:20 UTC" "2014-06-30 19:30:20 UTC"
#> [3] NA                        "2014-06-29 00:00:00 UTC"
#> [5] NA                       

# hms ---
## 12:13:14
x <- 0.50918982
convert_hms(x)
#> [1] "12:13:14"