Determine the Quarter from a date

Viewed 33

Let say I have date as follows:

Date = as.Date('2020-11-30')

Now I want to determine the quarter for this date, So I can use the zoo package

library(zoo)
as.yearqtr(Date). ### [1] "2020 Q4"

However I want to determine the quarter with respect to a date, say

Date1 = as.Date("2020-05-31")

So with respect to this date, the quarter of Date should be Q2.

Is there any way to set up the base in the quarter calculation?

Any pointer will be highly appreciated.

Thanks,

1 Answers

if we want to extract the quarter, use format

format(as.yearqtr(Date1), 'Q%q')
[1] "Q2"

Or if it is based on difference, try

paste0("Q", (as.yearqtr(Date) -  as.yearqtr(Date1)) * 4)
[1] "Q2"
Related