【问题标题】:adding three months to the current month in js [duplicate]在js中向当前月份添加三个月[重复]
【发布时间】:2013-05-27 05:08:37
【问题描述】:

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

new Date().getFullYear()

我想要的是将当前日期添加三个月并检查年份是否更改!在java中我们可以做到这一点,但我不知道如何做这个javascript。如何使用 javascript 检查这种情况!

【问题讨论】:

标签: javascript jquery


【解决方案1】:
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");

【讨论】:

  • @Juhana 对不起,如果我的观点是错误的
  • 为什么要投反对票。请给我理由
  • 请学会在投反对票之前发表评论
  • @downvoter:你能解释一下原因吗?
【解决方案2】:

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

var k = new Date();

k.setMonth(k.getMonth()+3);

alert(k);

这是demo fiddle

【讨论】:

    猜你喜欢
    • 2020-07-26
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    相关资源
    最近更新 更多