How to add weeks to date using javascript?

前端 未结 8 850
轻奢々
轻奢々 2020-12-29 18:32

Javascript definitely isn\'t my strongest point. I\'ve been attempting this for a couple of hours now and seem to be getting stuck with date formatting somewhere.

I

8条回答
  •  我在风中等你
    2020-12-29 18:57

    You can do this :

    let numWeeks = 2;
    let now = new Date();
    now.setDate(now.getDate() + numWeeks * 7);
    alert(now);
    

    You can see the fiddle here.

    According to the documentation in MDN

    The setDate() method sets the day of the Date object relative to the beginning of the currently set month.

提交回复
热议问题