<html>
<head>
<title>js 两个日期间隔月数</title>
<script type="text/javascript">
window.onload = function () {
document.getElementById("btn_Diff").onclick = function () {
var d1 = document.getElementById("txt_Date1").value//日期1
var d2 = document.getElementById("txt_Date2").value//日期2
//年*12+月
var m1 = parseInt(d1.split("-")[1].replace(/^0+/, "")) + parseInt(d1.split("-")[0]) * 12
var m2 = parseInt(d2.split("-")[1].replace(/^0+/, "")) + parseInt(d2.split("-")[0]) * 12
alert(m2-m1)
}
}
</script>
</head>
<body>
<input id="txt_Date1" type="text" value="2013-09-25"/>
<input id="txt_Date2" type="text" value="2014-01-15"/>
<input id="btn_Diff" type="button" value="Diff"/>
</body>
</html>
参考原文链接: https://blog.csdn.net/qq_32584661/article/details/81632920?spm=1001.2101.3001.6650.6&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-6-81632920-blog-124099642.pc_relevant_antiscanv4&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-6-81632920-blog-124099642.pc_relevant_antiscanv4&utm_relevant_index=10