在js中向当前月份添加三个月

adding three months to the current month in js

本文关键字:添加 三个月 js      更新时间:2023-09-26

我使用了以下Javascript代码来获取当前年份。

new Date().getFullYear()

我想要的是在当前日期上添加三个月,并检查年份是否变化!在java中,我们可以做到这一点,但我不知道如何做这个javascript。我如何用javascript检查这个条件!

var n1 = new Date().getFullYear();
var presentDate= new Date();
presentDate.setMonth(presentDate.getMonth()+No Of months you want to add);
var n2 = presentDate.getFullYear();
 if(n1 == n2)
    alert("Year not changed");
   else
    alert("Year changed");

要将日期对象移动到3个月前,可以使用setMonth函数。

var k = new Date();
k.setMonth(k.getMonth()+3);
alert(k);