js获取当前指定的前几天的日期(如当前时间的前七天的日期)
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
function getBeforeDate(n){
var n = n;
var d = new Date();
var year = d.getFullYear();
var mon=d.getMonth()+1;
var day=d.getDate();
if(day <= n){
if(mon>1) {
mon=mon-1;
}
else {
year = year-1;
mon = 12;
}
}
d.setDate(d.getDate()-n);
year = d.getFullYear();
mon=d.getMonth()+1;
day=d.getDate();
s = year+"-"+(mon<10?(0+mon):mon)+"-"+(day<10?(0+day):day);
return s;
}
console.log(getBeforeDate(1));//昨天的日期
console.log(getBeforeDate(7));//前七天的日期
</script>
</body>
</html>
__EOF__
共有 0 条评论