Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/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 - Fatal编程技术网

Javascript 如何获取当前年份已过去的月数(相对于当前日期)?

Javascript 如何获取当前年份已过去的月数(相对于当前日期)?,javascript,Javascript,到目前为止,我只知道如何获得本年度: const date = new Date() const year = date.getFullYear() => 2017 <- find how many months have passed in this year relative with the current date 找出今年相对于 当前日期 只需执行date.getMonths var monthsElapsed = date.getMonth() + 1; 请分享更多

到目前为止,我只知道如何获得本年度:

const date = new Date()
const year = date.getFullYear() 
=> 2017 <- find how many months have passed in this year relative with the current date
找出今年相对于 当前日期

只需执行
date.getMonths

var monthsElapsed = date.getMonth() + 1;

请分享更多的输入和输出示例。您尝试和研究了哪些内容?一般来说,询问没有预期输出的代码也是如此,而不是在没有亲自尝试任何代码的情况下提问。@gurvinder372没有输入。只是我提供的代码。(我添加了一些输出)。你能不能只做
date.getMonth()+1
?但2018年:P
var monthsElapsed = date.getMonth() + 1;