<script>
//通过传入要正整数获得前一N天的日期,传入负数为当前日期的未来N天日期
function getDate(day) {
var zdate = new Date();
var sdate = zdate.getTime() - (1 * 24 * 60 * 60 * 1000);
var edate = new Date(sdate - (day * 24 * 60 * 60 * 1000));
return formatDate(edate);
}
/*格式化日期为yyyy-MM-dd*/
function formatDate(date) {
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
return year + "-" + formatTen(month) + "-" + formatTen(day);
}
//当小于10为数字前补0
function formatTen(num) {
return num > 9 ? (num + "") : ("0" + num);
}
//打印到界面
document.write(getDate(6));
</script>
转载于:https://my.oschina.net/u/2252639/blog/663941