It just seems strange to me, comparing two dates, that the output format would include hours, minutes and seconds. It makes sense in the last example below where I've provided now() - a datetime - but when it's just two dates... I can't think of an example where the return wouldn't be a whole number of days.

library(lubridate)
as.period(
  interval(ymd("1982-08-10"), today()),
  unit = "day"
)
#> [1] "13933d 0H 0M 0S"
as.period(
  interval(ymd("1982-08-10"), today()),
  unit = "year"
)
#> [1] "38y 1m 22d 0H 0M 0S"
as.period(
  interval(ymd("1982-08-10"), now()),
  unit = "year"
)
#> [1] "38y 1m 22d 13H 54M 30.653412103653S"

Created on 2020-10-02 by the reprex package (v0.3.0)

1 Like

Hi @francisbarton,

the question is well asked. And I think, you're right; it does not make sense. But I think it's the standard format which the lubridate makers chose for (what they call) periods. Maybe to make the difference to a duration.

You could use base R's difftime which is in days.

Example

library(lubridate)
as.difftime(ymd("2022-10-20") - today())
#> Time difference of 748 days
as.difftime(ymd_h("2022-10-20 0") - now())
#> Time difference of 747.1813 days

# the base unit is days for difftime
as.numeric(as.difftime(ymd_h("2022-10-20 0") - now()))
#> [1] 747.1813

# while the base unit for period is the second
as.numeric(as.period(ymd("2022-10-20") - today()))
#> [1] 64627200

Created on 2020-10-02 by the reprex package (v0.3.0)

The main difference to durations occurs when we try to display your example in months which usually have an unspecific length

as.period(
  interval(ymd("1982-08-10"), today()),
  unit = "months"
)
#> [1] "457m 22d 0H 0M 0S"

as.period(
  interval(ymd("1982-08-10"), today()),
  unit = "days"
)
#> [1] "13933d 0H 0M 0S"

We learn that an average month here has this many days:

(13933 - 22) / 457
#> [1] 30.43982

:slight_smile:

2 Likes

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.