Generate sequences of anniversary dates between 2 dates
NickName:southwind Ask DateTime:2018-12-13T09:25:14

Generate sequences of anniversary dates between 2 dates

I tried to generate a sequence of dates between two dates. By search all the old posts, I found very nice solution using seq.Date.

For example:

> seq.Date(as.Date("2016/1/15"), as.Date("2016/5/1"), by = "month")
[1] "2016-01-15" "2016-02-15" "2016-03-15" "2016-04-15"

The above function yields very nice solution. However, it doesnt work when the date is 30 or 31 in Jan.

> seq.Date(as.Date("2016/1/30"), as.Date("2016/5/1"), by = "month")
[1] "2016-01-30" "2016-03-01" "2016-03-30" "2016-04-30"

The second anniversary jumps to March instead of being capped at 29/Feb. I couldnt find a workaround for this.

Copyright Notice:Content Author:「southwind」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53753751/generate-sequences-of-anniversary-dates-between-2-dates

Answers
Julius Vainora 2018-12-13T02:23:32

Here's an approach that also works in other cases:\n\nlibrary(lubridate)\nfun <- function(from, to, by) {\n mySeq <- seq.Date(as.Date(from), as.Date(to), by = by)\n as.Date(sapply(mySeq, function(d) d + 1 - which.max(day(d - 0:3))), origin = \"1970-01-01\")\n}\n\nfun(\"2016/1/30\", \"2016/5/1\", \"month\")\n# [1] \"2016-01-30\" \"2016-02-29\" \"2016-03-30\" \"2016-04-30\"\nfun(\"2017/1/31\", \"2017/5/1\", \"month\")\n# [1] \"2017-01-31\" \"2017-02-28\" \"2017-03-31\" \"2017-04-30\"\nfun(\"2017/1/29\", \"2017/5/1\", \"month\")\n# [1] \"2017-01-29\" \"2017-02-28\" \"2017-03-29\" \"2017-04-29\"\n\n\nWhat fun does is that it subtracts 0:3 from each date and chooses the one that has the largest day.",


More about “Generate sequences of anniversary dates between 2 dates” related questions

Generate sequences of anniversary dates between 2 dates

I tried to generate a sequence of dates between two dates. By search all the old posts, I found very nice solution using seq.Date. For example: &gt; seq.Date(as.Date("2016/1/15"), as.Date("2016/5...

Show Detail

Oracle SQL Anniversary Dates Query

I need some assistance in creating a query which shows the anniversary of employees in the business. I need the report to run and show results within two dates I select. I would like the report to ...

Show Detail

Generate List of dates between 2 dates for each Id

I have a table with PersonId's that each have a FirstSubscription date and LastSubscriptionDate. What I need to do is between those 2 dates, generate 1 date for each month. This is for reporting pu...

Show Detail

How do I generate all the dates of Sunday between 2 dates in oracle sql?

How do I generate all the dates of Sunday between 2 dates in oracle SQL? Example if I want all the Sundays between "01/10/2018" and "31/12/2018" the output will be: 07/10/2018 14/10/2018 21/10/2...

Show Detail

I need to compare 2 lists of dates and generate dates in between in Excel

I have 2 lists of dates. I want to find the latest date from list 1 and the earliest date from list 2, and then generate all dates that fit in between them. So I would like find a formula that wil...

Show Detail

Generate the dates in between Start and End Dates

I need to generate all dates in between two given dates. Here is the problem statement: Input: START_DATE END_DATE ---------- ---------- 01-FEB-16 03-FEB-16 01-FEB-16 04-FEB-16 01-F...

Show Detail

Postgresql between dates

i am learning SQL at the moment and i was wondering if somebody could help me. I´m working with postgresql and my task ist to write a query which shows all the, in this case, employees having their...

Show Detail

Generate dates in each row between two dates in Excel

For the example below, I want to generate rows for each product as much as the dates between &quot;Date 2&quot; and &quot;Date 1&quot; and have a fourth column to include each unique day between &q...

Show Detail

Generate dates in each row between two dates in Excel

For the example below, I want to generate rows for each product as much as the dates between &quot;Date 2&quot; and &quot;Date 1&quot; and have a fourth column to include each unique day between &q...

Show Detail

Generate dates between range in Excel

Is it possible to automatically (without user interaction) generate dates between two that specify range? Let's say that we need to insert start and end date in two cells, and that Excel autofill ...

Show Detail