Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the difference between dates in terms of weeks, months, quarters, and years

Tags:

date

r

I have two dates let´s say 14.01.2013 and 26.03.2014.

I would like to get the difference between those two dates in terms of weeks(?), months(in the example 14), quarters(4) and years(1).

Do you know the best way to get this?

like image 726
ddg Avatar asked Jan 22 '13 08:01

ddg


People also ask

How do you find the date difference between days months and years?

Select a blank cell which will place the calculated result, enter this formula =DATEDIF(A2,B2,"Y") & " Years, " & DATEDIF(A2,B2,"YM") & " Months, " & DATEDIF(A2,B2,"MD") & " Days", press Enter key to get the result.

How do you calculate the difference in weeks between two dates?

To calculate the number of weeks between two dates, start by counting the number of days between the start and end date. Then, divide that number by 7 days per week. How many days are between two dates?


Video Answer


1 Answers

what about this:

# get difference between dates `"01.12.2013"` and `"31.12.2013"`  # weeks difftime(strptime("26.03.2014", format = "%d.%m.%Y"), strptime("14.01.2013", format = "%d.%m.%Y"),units="weeks") Time difference of 62.28571 weeks  # months (as.yearmon(strptime("26.03.2014", format = "%d.%m.%Y"))- as.yearmon(strptime("14.01.2013", format = "%d.%m.%Y")))*12 [1] 14  # quarters (as.yearqtr(strptime("26.03.2014", format = "%d.%m.%Y"))- as.yearqtr(strptime("14.01.2013", format = "%d.%m.%Y")))*4 [1] 4  # years year(strptime("26.03.2014", format = "%d.%m.%Y"))- year(strptime("14.01.2013", format = "%d.%m.%Y")) [1] 1 

as.yearmon() and as.yearqtr() are in package zoo. year() is in package lubridate. What do you think?

like image 57
ddg Avatar answered Oct 30 '22 17:10

ddg