js获取8个月前时间,1天前时间

一、获取八个月前时间

//获取8个月前时间
//小于10的前面加0
function p(s) {
    return s < 10 ? '0' + s: s;
}

function getEightMonth(){
    var myDate = new Date();
    var currentYear=myDate.getFullYear();
    var currentMonth=myDate.getMonth()+1;  
    var currentDate=myDate.getDate();

         //八个月前
         var eightY=0;
         var eightM=0;
         var eigthD=0;

    var daysInMonth = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
    isYears(currentYear);
         if(currentMonth==1){

      //8个月前
        eightY= currentYear-1;
        eightM=5;
        eightD=monthTime(eightM,currentDate);
 
         }else{
            //8个月前的时间
        if(currentMonth -8 < 1){
            eightY = currentYear-1;
            eightM = isZero(12+(currentMonth-8));
            isZero(eightM);
            isYears(eightM);
            eightD =monthTime(eightM,currentDate)
        }else{
            eightY = currentYear;
            eightM = isZero(currentMonth-8);
            isZero(eightM);
            isYears(eightM);
            eightD =monthTime(eightM,currentDate)
        }
 }
         
     function isYears(years){
         if(years%4 == 0 && years%100 != 0  || years%400 == 0 ){
                daysInMonth[2]= 29;
            }
     }
     function monthTime(a,b){
         if(daysInMonth[a] < b){
                 lastDate =  daysInMonth[a]
            }else{
                 lastDate = b;                
            }
         return lastDate;
     } 
     
     function isZero(s){
         if(s ==0 ){
             return s =12;
         }else{
             return s;
         }
     }
    //八个月前
 var eightMonth=eightY+"-"+p(eightM)+"-"+p(eightD);
 return eightMonth;
}

 

二、获取一天后的日期

//24小时后
var dDate=new Date();
function getOneDay(){
    var nextDate = new Date(dDate.getTime() + 24*60*60*1000);   
    var year = nextDate.getFullYear();
    
    var oldmonth = nextDate.getMonth();
    var finalOldMonth=Appendzero(oldmonth);
    var month = nextDate.getMonth()+1;
    var finalMonth=Appendzero(month);
    
    var strDate = nextDate.getDate(nextDate);
    var finalStrDate=Appendzero(strDate);
    var OneDay= year+'-'+finalMonth+'-'+finalStrDate;
    return OneDay;

}

 

posted @ 2018-12-10 17:26  pheosia  阅读(474)  评论(0编辑  收藏  举报