js获取N天后的日期


这个javascript函数是获取当前时间前后N天日期的方法,可以得到昨天,今天,明天,一月前,三月前,半年前,一年前的日期,只要修改参数就可以实现得到任何一个天数。具体用法如下:

  function GetDateStr(AddDayCount) {   

    var dd = new Date();  

    dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期  

    var y = dd.getFullYear();   

    var m = (dd.getMonth()+1)<10?"0"+(dd.getMonth()+1):(dd.getMonth()+1);//获取当前月份的日期,不足10补0  

    var d = dd.getDate()<10?"0"+dd.getDate():dd.getDate();//获取当前几号,不足10补0  

    return y+"-"+m+"-"+d;   

  }  

 

console.log("半年前:"+GetDateStr(-180));  

console.log("三月前:"+GetDateStr(-90));  

console.log("一月前:"+GetDateStr(-30));  

console.log("昨天:"+GetDateStr(-1));  

console.log("今天:"+GetDateStr(0));  

console.log("明天:"+GetDateStr(1));  

console.log("后天:"+GetDateStr(2));  

console.log("一月后:"+GetDateStr(30));  

console.log("三月后:"+GetDateStr(90));  

console.log("半年后:"+GetDateStr(180));   


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM