js 获取指定日期,剩余天数

1.获取天数 如昨天,今天,近1星期,近14天

// 如昨天 GetDateStr(-1)
function GetDateStr(AddDayCount) {  
        var dd = new Date(); 
        dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期 
        var y = dd.getFullYear(); 
        var m = dd.getMonth()+1;//获取当前月份的日期 
        var d = dd.getDate(); 
        //判断 月
        if(m < 10){
            m = "0" + m;
        }else{
            m = m;
        }
        //判断 日n 
        if(d < 10){
  //如果天数<10
            d = "0" + d;
        }else{
            d = d;
        }
        return y+"-"+m+"-"+d; 
    }        

2.获取月份 如1个月前GetMonthStr(-1),3个月前,6个月前

//如1个月前 GetMonthStr(-1)
function GetMonthStr(AddMonthCount) {  
        var dd = new Date(); 
        dd.setMonth(dd.getMonth()+AddMonthCount);//获取AddMonthCount月后的日期 
        var y = dd.getFullYear(); 
        var m = dd.getMonth()+1;//获取当前月份的日期 
        var d = dd.getDate(); 
        //判断 月
        if(m < 10){
            m = "0" + m;
        }else{
            m = m;
        }
        //判断 日n 
        if(d < 10){
  //如果天数<10
            d = "0" + d;
        }else{
            d = d;
        }
        return y+"-"+m+"-"+d; 
    } 

3.获取年 如1个年前 GetYearStr(-1)

// 如1个年前 GetYearStr(-1)
function GetYearStr(AddYearCount) {  
        var dd = new Date(); 
        dd.setYear(dd.getFullYear()+AddYearCount);//获取AddMonthCount月后的日期 
        var y = dd.getFullYear(); 
        var m = dd.getMonth()+1;//获取当前月份的日期 
        var d = dd.getDate(); 
        //判断 月
        if(m < 10){
            m = "0" + m;
        }else{
            m = m;
        }
        //判断 日n 
        if(d < 10){
  //如果天数<10
            d = "0" + d;
        }else{
            d = d;
        }
        return y+"-"+m+"-"+d; 
    }

剩余天数

    /*剩余天数*/
    var surplusDays;
    //总天数
    var dayToal;
    //当前日期
    var date = new Date();
    //当前天数
    var curTime = new Date().getDate();
    var year = date.getFullYear();
    var mouth = date.getMonth() + 1;
    if(mouth == 2){
        dayToal= year % 4 == 0 ? 29 : 28;  
    }else if(mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12){
        dayToal= 31;
    }else{
        dayToal= 30;
    }
    surplusDays = dayToal - curTime;

计费方式-购置月末剩余天数

//这里是购置月末的剩余天数 
function getDays(){
    var dayToal;
    var date = new Date();
    var curTime = new Date().getDate();
    var year = date.getFullYear();
    var mouth = date.getMonth() + 1;
    if(mouth == 2){
        dayToal= year % 4 == 0 ? 29 : 28;
    }else if(mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12){
        dayToal= 31;
    }else{
        dayToal= 30;
    }
    day = ((dayToal-curTime)+1)/dayToal;
}
    原文作者:sx_lidan
    原文地址: https://blog.csdn.net/sx_lidan/article/details/70046291
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞