Using javascript get every other Sunday matching a particular week










1















Using javascript I need to get the next upcoming date that matches a particular pattern.



The date I need is a Sunday and I can get the next Sunday like this:



const date = new Date();
const diff = date.getDay() - 7;
if (diff > 0)
date.setDate(date.getDate() + 6);
else if (diff < 0)
date.setDate(date.getDate() + ((-1) * diff));

console.log(date);


But the Sunday I need to get, is every other Sunday matching the following pattern.



10/20/18
11/03/18
11/17/18
12/01/18
12/15/18










share|improve this question



















  • 3





    So what's the problem? Get the first one and keep on adding 14 days.

    – Robby Cornelissen
    Nov 16 '18 at 5:38











  • But days in your pattern are saturdays...

    – barbsan
    Nov 16 '18 at 7:43















1















Using javascript I need to get the next upcoming date that matches a particular pattern.



The date I need is a Sunday and I can get the next Sunday like this:



const date = new Date();
const diff = date.getDay() - 7;
if (diff > 0)
date.setDate(date.getDate() + 6);
else if (diff < 0)
date.setDate(date.getDate() + ((-1) * diff));

console.log(date);


But the Sunday I need to get, is every other Sunday matching the following pattern.



10/20/18
11/03/18
11/17/18
12/01/18
12/15/18










share|improve this question



















  • 3





    So what's the problem? Get the first one and keep on adding 14 days.

    – Robby Cornelissen
    Nov 16 '18 at 5:38











  • But days in your pattern are saturdays...

    – barbsan
    Nov 16 '18 at 7:43













1












1








1








Using javascript I need to get the next upcoming date that matches a particular pattern.



The date I need is a Sunday and I can get the next Sunday like this:



const date = new Date();
const diff = date.getDay() - 7;
if (diff > 0)
date.setDate(date.getDate() + 6);
else if (diff < 0)
date.setDate(date.getDate() + ((-1) * diff));

console.log(date);


But the Sunday I need to get, is every other Sunday matching the following pattern.



10/20/18
11/03/18
11/17/18
12/01/18
12/15/18










share|improve this question
















Using javascript I need to get the next upcoming date that matches a particular pattern.



The date I need is a Sunday and I can get the next Sunday like this:



const date = new Date();
const diff = date.getDay() - 7;
if (diff > 0)
date.setDate(date.getDate() + 6);
else if (diff < 0)
date.setDate(date.getDate() + ((-1) * diff));

console.log(date);


But the Sunday I need to get, is every other Sunday matching the following pattern.



10/20/18
11/03/18
11/17/18
12/01/18
12/15/18







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 6:48









Krupesh Kotecha

2,07311136




2,07311136










asked Nov 16 '18 at 5:33









Jared WhippleJared Whipple

3402626




3402626







  • 3





    So what's the problem? Get the first one and keep on adding 14 days.

    – Robby Cornelissen
    Nov 16 '18 at 5:38











  • But days in your pattern are saturdays...

    – barbsan
    Nov 16 '18 at 7:43












  • 3





    So what's the problem? Get the first one and keep on adding 14 days.

    – Robby Cornelissen
    Nov 16 '18 at 5:38











  • But days in your pattern are saturdays...

    – barbsan
    Nov 16 '18 at 7:43







3




3





So what's the problem? Get the first one and keep on adding 14 days.

– Robby Cornelissen
Nov 16 '18 at 5:38





So what's the problem? Get the first one and keep on adding 14 days.

– Robby Cornelissen
Nov 16 '18 at 5:38













But days in your pattern are saturdays...

– barbsan
Nov 16 '18 at 7:43





But days in your pattern are saturdays...

– barbsan
Nov 16 '18 at 7:43












2 Answers
2






active

oldest

votes


















2














using momentjs it's easy like this:



moment().day(-7) // returns last Sunday
moment().day(0) // returns this Sunday
moment().day(7) // returns next Sunday object
moment().day(14) // returns next+1 Sunday object


http://momentjs.com/docs/#/get-set/day/



if you need a JS Date object:



moment().day(7).toDate()





share|improve this answer























  • I see, understood. But maybe adding a momentjs is not a problem. At least would be good to know how it helps. I hope

    – Roman86
    Nov 16 '18 at 6:13











  • I think it's a grey area. But since there are so many useful libraries out there I think it's helpful to get recommendations. That being said programers in general tend to rely on libraries a bit too much sometimes.

    – Jared Whipple
    Nov 16 '18 at 16:58


















0














This is what I came up with using just javascript. There is probably a better more efficient way of doing it.



function getDateArray(start, end = new Date()) 
const arr = new Array();
const sd = new Date(start);
while (sd <= end)
const d = new Date(sd);
const d2 = new Date(d);
d2.setDate(d2.getDate() + 13);
const v = d.valueOf();
const ds1 = d.toDateString();
const ds2 = d2.toDateString();
const obj = label: `From: $ds1 To: $ds2`, value: v ;
arr.push(obj);
sd.setDate(sd.getDate() + 14);

return arr;


const today = new Date();
getDateArray('08/19/18', today);


I can get the date I need by accessing the last item in the returned array






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332004%2fusing-javascript-get-every-other-sunday-matching-a-particular-week%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    using momentjs it's easy like this:



    moment().day(-7) // returns last Sunday
    moment().day(0) // returns this Sunday
    moment().day(7) // returns next Sunday object
    moment().day(14) // returns next+1 Sunday object


    http://momentjs.com/docs/#/get-set/day/



    if you need a JS Date object:



    moment().day(7).toDate()





    share|improve this answer























    • I see, understood. But maybe adding a momentjs is not a problem. At least would be good to know how it helps. I hope

      – Roman86
      Nov 16 '18 at 6:13











    • I think it's a grey area. But since there are so many useful libraries out there I think it's helpful to get recommendations. That being said programers in general tend to rely on libraries a bit too much sometimes.

      – Jared Whipple
      Nov 16 '18 at 16:58















    2














    using momentjs it's easy like this:



    moment().day(-7) // returns last Sunday
    moment().day(0) // returns this Sunday
    moment().day(7) // returns next Sunday object
    moment().day(14) // returns next+1 Sunday object


    http://momentjs.com/docs/#/get-set/day/



    if you need a JS Date object:



    moment().day(7).toDate()





    share|improve this answer























    • I see, understood. But maybe adding a momentjs is not a problem. At least would be good to know how it helps. I hope

      – Roman86
      Nov 16 '18 at 6:13











    • I think it's a grey area. But since there are so many useful libraries out there I think it's helpful to get recommendations. That being said programers in general tend to rely on libraries a bit too much sometimes.

      – Jared Whipple
      Nov 16 '18 at 16:58













    2












    2








    2







    using momentjs it's easy like this:



    moment().day(-7) // returns last Sunday
    moment().day(0) // returns this Sunday
    moment().day(7) // returns next Sunday object
    moment().day(14) // returns next+1 Sunday object


    http://momentjs.com/docs/#/get-set/day/



    if you need a JS Date object:



    moment().day(7).toDate()





    share|improve this answer













    using momentjs it's easy like this:



    moment().day(-7) // returns last Sunday
    moment().day(0) // returns this Sunday
    moment().day(7) // returns next Sunday object
    moment().day(14) // returns next+1 Sunday object


    http://momentjs.com/docs/#/get-set/day/



    if you need a JS Date object:



    moment().day(7).toDate()






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 5:57









    Roman86Roman86

    846610




    846610












    • I see, understood. But maybe adding a momentjs is not a problem. At least would be good to know how it helps. I hope

      – Roman86
      Nov 16 '18 at 6:13











    • I think it's a grey area. But since there are so many useful libraries out there I think it's helpful to get recommendations. That being said programers in general tend to rely on libraries a bit too much sometimes.

      – Jared Whipple
      Nov 16 '18 at 16:58

















    • I see, understood. But maybe adding a momentjs is not a problem. At least would be good to know how it helps. I hope

      – Roman86
      Nov 16 '18 at 6:13











    • I think it's a grey area. But since there are so many useful libraries out there I think it's helpful to get recommendations. That being said programers in general tend to rely on libraries a bit too much sometimes.

      – Jared Whipple
      Nov 16 '18 at 16:58
















    I see, understood. But maybe adding a momentjs is not a problem. At least would be good to know how it helps. I hope

    – Roman86
    Nov 16 '18 at 6:13





    I see, understood. But maybe adding a momentjs is not a problem. At least would be good to know how it helps. I hope

    – Roman86
    Nov 16 '18 at 6:13













    I think it's a grey area. But since there are so many useful libraries out there I think it's helpful to get recommendations. That being said programers in general tend to rely on libraries a bit too much sometimes.

    – Jared Whipple
    Nov 16 '18 at 16:58





    I think it's a grey area. But since there are so many useful libraries out there I think it's helpful to get recommendations. That being said programers in general tend to rely on libraries a bit too much sometimes.

    – Jared Whipple
    Nov 16 '18 at 16:58













    0














    This is what I came up with using just javascript. There is probably a better more efficient way of doing it.



    function getDateArray(start, end = new Date()) 
    const arr = new Array();
    const sd = new Date(start);
    while (sd <= end)
    const d = new Date(sd);
    const d2 = new Date(d);
    d2.setDate(d2.getDate() + 13);
    const v = d.valueOf();
    const ds1 = d.toDateString();
    const ds2 = d2.toDateString();
    const obj = label: `From: $ds1 To: $ds2`, value: v ;
    arr.push(obj);
    sd.setDate(sd.getDate() + 14);

    return arr;


    const today = new Date();
    getDateArray('08/19/18', today);


    I can get the date I need by accessing the last item in the returned array






    share|improve this answer



























      0














      This is what I came up with using just javascript. There is probably a better more efficient way of doing it.



      function getDateArray(start, end = new Date()) 
      const arr = new Array();
      const sd = new Date(start);
      while (sd <= end)
      const d = new Date(sd);
      const d2 = new Date(d);
      d2.setDate(d2.getDate() + 13);
      const v = d.valueOf();
      const ds1 = d.toDateString();
      const ds2 = d2.toDateString();
      const obj = label: `From: $ds1 To: $ds2`, value: v ;
      arr.push(obj);
      sd.setDate(sd.getDate() + 14);

      return arr;


      const today = new Date();
      getDateArray('08/19/18', today);


      I can get the date I need by accessing the last item in the returned array






      share|improve this answer

























        0












        0








        0







        This is what I came up with using just javascript. There is probably a better more efficient way of doing it.



        function getDateArray(start, end = new Date()) 
        const arr = new Array();
        const sd = new Date(start);
        while (sd <= end)
        const d = new Date(sd);
        const d2 = new Date(d);
        d2.setDate(d2.getDate() + 13);
        const v = d.valueOf();
        const ds1 = d.toDateString();
        const ds2 = d2.toDateString();
        const obj = label: `From: $ds1 To: $ds2`, value: v ;
        arr.push(obj);
        sd.setDate(sd.getDate() + 14);

        return arr;


        const today = new Date();
        getDateArray('08/19/18', today);


        I can get the date I need by accessing the last item in the returned array






        share|improve this answer













        This is what I came up with using just javascript. There is probably a better more efficient way of doing it.



        function getDateArray(start, end = new Date()) 
        const arr = new Array();
        const sd = new Date(start);
        while (sd <= end)
        const d = new Date(sd);
        const d2 = new Date(d);
        d2.setDate(d2.getDate() + 13);
        const v = d.valueOf();
        const ds1 = d.toDateString();
        const ds2 = d2.toDateString();
        const obj = label: `From: $ds1 To: $ds2`, value: v ;
        arr.push(obj);
        sd.setDate(sd.getDate() + 14);

        return arr;


        const today = new Date();
        getDateArray('08/19/18', today);


        I can get the date I need by accessing the last item in the returned array







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 17:10









        Jared WhippleJared Whipple

        3402626




        3402626



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332004%2fusing-javascript-get-every-other-sunday-matching-a-particular-week%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党