有些时候 要获取有些时间,例如最近7天/最近30天啦。。。and so on..
具体方法示例:
<script language="javascript" type="text/javascript"> function GetDateStr(AddDayCount) { var dd = new Date(); dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期 var y = dd.getYear(); 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; } document.write("半年前:"+GetDateStr(-180)); document.write("<br />三月前:"+GetDateStr(-90)); document.write("<br />一月前:"+GetDateStr(-30)); document.write("<br />昨天:"+GetDateStr(-1)); document.write("<br />今天:"+GetDateStr(0)); document.write("<br />明天:"+GetDateStr(1)); document.write("<br />后天:"+GetDateStr(2)); document.write("<br />一月后:"+GetDateStr(30)); document.write("<br />三月后:"+GetDateStr(90)); document.write("<br />半年后:"+GetDateStr(180)); </script>
转载请注明:苏demo的别样人生 » js获取当前日期的前后N天日期的方法