ronnydw
ronnydw

Reputation: 953

How can I calculate the difference between 2 dates in weeks?

How can I use lubridate functions to calculate the difference between 2 dates in weeks?

Upvotes: 2

Views: 5928

Answers (2)

ronnydw
ronnydw

Reputation: 953

I found an easier way without using lubridate:

t1 <- as.POSIXct("2000-01-01")
t2 <- as.POSIXct("2016-01-01")
as.double(difftime(t2,t1,unit="weeks"))

[1] 834.8571

Upvotes: 4

Ram K
Ram K

Reputation: 1785

One way to do this exclusively using the lubridate package :

library(lubridate)

# Create sample interval
> span <- interval(as.POSIXct("2000-01-01"), as.POSIXct("2016-01-01"))
> span
[1] 2000-01-01 PST--2016-01-01 PST

> t <- as.period(span, unit="day")
> t
[1] "5844d 0H 0M 0S"

> t / as.period(dweeks(1))
estimate only: convert durations to intervals for accuracy
estimate only: convert to intervals for accuracy
[1] 834.8571
> 

Upvotes: 4

Related Questions