首页 > Web开发 > 详细

转:JS获取前七天的日期

时间:2018-10-10 21:38:47      阅读:319      评论:0      收藏:0      [点我收藏+]

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>时间</title>
</head>
<body>
<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(2));

console.log(getBeforeDate(3));

console.log(getBeforeDate(4));

console.log(getBeforeDate(5));

console.log(getBeforeDate(6));
console.log(getBeforeDate(7));//前七天的日期
</script>
</body>
</html>

 

转:JS获取前七天的日期

原文:https://www.cnblogs.com/Ly426/p/9768662.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!