获取日期操作

(1)

value(3):月  startdate:上个月的第一天 enddate:上个月的最后一天

value(5):年  startdate:去年当前月的第一天 enddate:上个月的最后一天

function changestartorendtime(value){
	 computecycle = value;
	 var myDate = new Date();
	 var new_year = myDate.getFullYear();    //取当前的年份        
	 var new_month = myDate.getMonth();        
	 var new_enddate = new Date(new_year,new_month,1);
	 enddate = new Date(new_enddate.getTime()-1000*60*60*24).toLocaleDateString().replace(/[\/]/g,"-");
	 var new_yearstartdate = new Date(new_year-1,new_month,1).toLocaleDateString().replace(/[\/]/g,"-");
	 var new_monthstartdate = new Date(new_year,new_month-1,1).toLocaleDateString().replace(/[\/]/g,"-");
	 startdate=(value==5)?new_yearstartdate:new_monthstartdate;
}

 (2)

生成之前12个月的月份(YYYY-MM)

function gethistorymonth(){
    var myDate = new Date();
     var new_year = myDate.getFullYear()-1;    
     var new_month = myDate.getMonth()+1; 
     $("div[name=month]").empty();
     for(i = 0;i < 12;i++){
         if(new_month>12){
             new_year++;
             new_month=1;
         }
         var datestr = (new_month<10)?new_year+"-0"+new_month:new_year+"-"+new_month;
         new_month++;
         $("div[name=month]").append(datestr+"###");
     }
}

 

posted @ 2016-10-09 09:53  骑驴的是郭襄  阅读(136)  评论(0编辑  收藏  举报