8
$\begingroup$

How can I, in one line, generate all the dates with a constant day difference from one date to another?

For example, if I'm interested in dates in the interval staring from 24.4.2012 till 2015 and a 28 day difference.

$\endgroup$

4 Answers 4

11
$\begingroup$

Perhaps this way:

Table[DatePlus[{2012, 4, 24}, 28*i], {i, 1, 40}]

{{2012, 5, 22}, {2012, 6, 19}, {2012, 7, 17}, {2012, 8, 14}, {2012, 9, 11}, {2012, 10, 9}, {2012, 11, 6}, {2012, 12, 4}, {2013, 1, 1}, {2013, 1, 29}, {2013, 2, 26}, {2013, 3, 26}, {2013, 4, 23}, {2013, 5, 21}, {2013, 6, 18}, {2013, 7, 16}, {2013, 8, 13}, {2013, 9, 10}, {2013, 10, 8}, {2013, 11, 5}, {2013, 12, 3}, {2013, 12, 31}, {2014, 1, 28}, {2014, 2, 25}, {2014, 3, 25}, {2014, 4, 22}, {2014, 5, 20}, {2014, 6, 17}, {2014, 7, 15}, {2014, 8, 12}, {2014, 9, 9}, {2014, 10, 7}, {2014, 11, 4}, {2014, 12, 2}, {2014, 12, 30}, {2015, 1, 27}, {2015, 2, 24}, {2015, 3, 24}, {2015, 4, 21}, {2015, 5, 19}}

Of course you might want to finetune the number of 28-day intervals.

$\endgroup$
8
$\begingroup$

You can use the built in Calendar functions :

Needs["Calendar`"]

Floor[DaysBetween[{2012, 4, 24}, {2015, 12, 31}]/28]

(* 48 *)

NestList[DaysPlus[#, 28] &, {2012, 4, 24}, 48]
$\endgroup$
7
$\begingroup$

Single liner using currently available commands and avoiding the legacy issues.

NestList[DatePlus[#, 28] &, #1, 
   Round[DateDifference[##2]/28]] & @@ {{2012, 4, 24}, {2015, 12, 31}}
$\endgroup$
2
$\begingroup$

DateRange (for versions 9.0+):

enter image description here

So using {28, "Day"} as the third argument:

DateRange[{2012, 4, 24}, {2015}, {28, "Day"}][[All,;;3]]

we get

{{2012,4,24}, {2012,5,22}, {2012,6,19}, {2012,7,17}, {2012,8,14}, {2012,9,11},
{2012,10,9}, {2012,11,6}, {2012,12,4}, {2013,1,1}, {2013,1,29}, {2013,2,26},{2013,3,26},
{2013,4,23},{2013,5,21},{2013,6,18},{2013,7,16},{2013,8,13},{2013,9,10},{2013,10,8},
{2013,11,5},{2013,12,3},{2013,12,31},{2014,1,28},{2014,2,25},{2014,3,25},{2014,4,22},
{2014,5,20},{2014,6,17},{2014,7,15},{2014,8,12},{2014,9,9},{2014,10,7},{2014,11,4},
{2014,12,2},{2014,12,30}}

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.