JavaScript—获取当下往后七天的时间

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>获取当下往后七天的时间</title>
</head>
<body>
<script>
//方法一:借鉴来的
function getAfterDate(n) {
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth() + 1;
var day = d.getDate();
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
if(day <= n) {
if(month > 1) {
month = month + 1;
} else {
year = year + 1;
month = 12;
}
}
d.setDate(d.getDate() + n);
year = d.getFullYear();
month = d.getMonth() + 1;
day = d.getDate();
s = year + "-" + (month < 10 ? ('0' + month) : month) + "-" + (day < 10 ? ('0' + day) : day) + " " + hours + ":" + minutes + ":" + seconds;
return s;
}
console.log(getAfterDate(6));
// 方法二
var nowDate = new Date();
nowDate.setDate(nowDate.getDate() + 6);
var yy = nowDate.getFullYear();
var mm = nowDate.getMonth() + 1;
var dd = nowDate.getDate();
var hh = nowDate.getHours();
var mmi = nowDate.getMinutes();
var ss = nowDate.getSeconds();
var endD = yy + "-" + mm + "-" + dd + " " + hh + ":" + mmi + ":" + ss;
console.log(endD);
</script>
</body>
</html>
posted @ 2017-07-22 12:07  刘倩文  阅读(947)  评论(0编辑  收藏  举报