Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取上个月的当前月份打印_Javascript_Date - Fatal编程技术网

Javascript 获取上个月的当前月份打印

Javascript 获取上个月的当前月份打印,javascript,date,Javascript,Date,我想得到当前月份的数据。现在是五月,但奇怪的事情发生了: var thisDate = new Date(); console.log(thisDate); console.log(thisDate.getMonth()); 输出: Thu May 14 2020 12:25:19 4 预期值为5getMonth()返回值从0到11,因此只需将+1添加到该值。getMonth()返回值从0到11,因此只需将+1添加到该值。

我想得到当前月份的数据。现在是五月,但奇怪的事情发生了:

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

Thu May 14 2020 12:25:19
4
预期值为5

getMonth()
返回值从0到11,因此只需将+1添加到该值。

getMonth()
返回值从0到11,因此只需将+1添加到该值。

使用此代码

var thisDate = new Date();
console.log(thisDate); 
console.log(thisDate.getMonth() + 1); 
getMonth()方法根据本地时间返回指定日期内的月份,作为一个基于零的值(其中零表示一年中的第一个月)

使用此代码

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