欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js判断两个时间是否相差9个月问题代码

程序员文章站 2022-06-03 17:37:28
js判断两个时间是否相差9个月问题代码实现 function timedifference(date1, date2){ var newyear = date1.get...

js判断两个时间是否相差9个月问题代码实现

function timedifference(date1, date2){
		var newyear = date1.getfullyear();
		var newmonth =date1.getmonth() + 9;
		console.log(newmonth)
		if(newmonth >= 11){
			newyear += 1;
			newmonth -= 11;
			date1.setfullyear(newyear);
			date1.setmonth(newmonth-1);
		}
		else{
			date1.setfullyear(newyear);
			date1.setmonth(newmonth);
		}
		if(date1.gettime() >= date2.gettime()){
			return true;
		}
		else{
			return false;
		}
	}
date1,date2都必须为date类型的,想计算差几个月只需要改变9就可以了,时间差在9个月内返回true,否则返回false