【问题标题】:get current month prints previos month [duplicate]获取当前月份打印上个月[重复]
【发布时间】:2020-05-14 09:34:04
【问题描述】:

我想获取当前月份。我们现在在五月,但发生了一件奇怪的事情:

var thisDate = new Date();
console.log(thisDate); 
console.log(thisDate.getMonth()); 

输出:

Thu May 14 2020 12:25:19
4

预期值为 5

【问题讨论】:

    标签: javascript date


    【解决方案1】:

    getMonth() 返回从 0 到 11 的数字,所以只需添加 +1 即可。

    【讨论】:

      【解决方案2】:

      getMonth() 方法根据本地时间返回指定日期中的月份,作为从零开始的值(其中零表示一年中的第一个月)。

      https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth

      使用此代码

      var thisDate = new Date();
      console.log(thisDate); 
      console.log(thisDate.getMonth() + 1); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-30
        • 2021-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多