美文网首页
js给日期加月数,类似于java的calendar

js给日期加月数,类似于java的calendar

作者: JamesPang_4841 | 来源:发表于2018-10-25 15:32 被阅读0次

    so上看到的实现方式:

    function addMonths(date, months) {

      var d = date.getDate();

      date.setMonth(date.getMonth() + +months);

      if (date.getDate() != d) {

        date.setDate(0);

      }

      return date;

    }

    // Add 12 months to 29 Feb 2016 -> 28 Feb 2017

    console.log(addMonths(new Date(2016,1,29),12).toString());

    // Subtract 1 month from 1 Jan 2017 -> 1 Dec 2016

    console.log(addMonths(new Date(2017,0,1),-1).toString());

    // Subtract 2 months from 31 Jan 2017 -> 30 Nov 2016

    console.log(addMonths(new Date(2017,0,31),-2).toString());

    // Add 2 months to 31 Dec 2016 -> 28 Feb 2017

    console.log(addMonths(new Date(2016,11,31),2).toString());

    相关文章

      网友评论

          本文标题:js给日期加月数,类似于java的calendar

          本文链接:https://www.haomeiwen.com/subject/pecctqtx.html