convert numeric to Date with default origin

Please consider the following:

as.numeric(as.Date("2016-12-06"))
[1] 17141

How do i convert 17141 back to a Date value?
as.Date(x,origin) will do this, but I don't know where to find a default origin.

The "epoch" is named in the documentation for as.POSIX, but it's definitely buried in there. I only found it because I knew it beforehand and used the find feature to search. Note that the epoch is defined just as an example, like it's not a useful thing to know.

Some of the concepts used have to be extended backwards in time (the usage is said to be ‘proleptic’). For example, the origin of time for the "POSIXct" class, ‘1970-01-01 00:00.00 UTC’, is before UTC was defined.

Anyways, you can always find the origin by taking a date (stored as number of days since the origin) and subtracting it's numeric form from itself. This will take you that many days backwards, leaving you at 0 days since the origin.

as.Date("2016-12-06") - as.numeric(as.Date("2016-12-06"))
# [1] "1970-01-01"

Thanks!

So as.Date could have been defined as:

as.Date<-function(x, origin=as.Date("1970-01-01") - as.numeric(as.Date("1970-01-01"), ...) {
...
}

Pity!

I generally use the lubridate package, which makes dealing with dates, date-times, and periods very easy. And it includes an origin object, too, and its commands have a tidyverse look, like as_date() in contrast to as.Date().

Ah, lubridate!

#S4 method for signature 'numeric'
as_date(x, origin = lubridate::origin)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.