js日期日和月相加减


function sub1(){ 
	var myDate = new Date();
	var enddate= "";
	var startdate= myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+(myDate.getDate());
	
	var y = myDate.getFullYear();
	var m = myDate.getMonth();
	var d = myDate.getDate();
	startdate =getthedate(y,m,d,0);
	enddate = myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+(myDate.getDate());  
	document.forms[0].elements[7].value=startdate; 
	document.forms[0].elements[8].value=enddate;
	document.forms[0].elements[9].click();
	
}function sub5(){ 
	var myDate = new Date();
	var enddate= "";
	var startdate= myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+(myDate.getDate());
	var y = myDate.getFullYear();
	var m = myDate.getMonth();
	var d = myDate.getDate();
	startdate = getmonthdate(y,m,d,-1);
	enddate = myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+(myDate.getDate());      
	document.forms[0].elements[7].value=startdate; 
	document.forms[0].elements[8].value=enddate;
	document.forms[0].elements[9].click();
	
} 
//得到最后一天
function getlastday(year,month)      
{      
 var new_year = year;    //取当前地年份      
 var new_month = month++;//取下一个月地第一天,方便计算(最后一天不固定)      
 if(month>12)            //如果当前大于12月,则年份转到下一年      
 {      
  new_month -=12;        //月份减      
  new_year++;            //年份增      
 }      
 var new_date = new Date(new_year,new_month,1);                //取当年当月中地第一天     
 return (new Date(new_date.getTime()-1000*60*60*24)).getDate();//获取当月最后一天日期     
}      

//得到日期(加减天数)
function getthedate(y,m,d,dadd)
{
//可以加上错误处理
var a = new Date(y,m,d);
a = a.valueOf()
a = a + dadd * 24 * 60 * 60 * 1000
a = new Date(a);
var m = a.getMonth() + 1;
if(m.toString().length == 1){
    m='0'+m;
}
var d = a.getDate();
if(d.toString().length == 1){
    d='0'+d;
}
return a.getFullYear() + "-" + m + "-" + d;
}

//得到日期(加减月份)
function getmonthdate(yy,m,d,dadd)
{
//可以加上错误处理
var a = new Date(yy,m,d)
a = a.valueOf()
a = new Date(a);
var m = a.getMonth() + 1 + dadd;
var y = a.getFullYear();
if(m>12){
	m=m-12;
	y = y+1;
} else if(m<=0){
    m = m+12; 
	y = y-1
  }   
if(m.toString().length == 1){
    m='0'+m;
}
var d = a.getDate();
if(d.toString().length == 1){
    d='0'+d;
}
return y + "-" + m + "-" + d;
}

你可能感兴趣的:(js)