码迷,mamicode.com
首页 > Web开发 > 详细

转:JS获取前七天的日期

时间:2018-10-10 22:02:00      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:idt   pat   charset   html   rip   maximum   webkit   for   获取   

<!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获取前七天的日期

标签:idt   pat   charset   html   rip   maximum   webkit   for   获取   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!