1.设置日期,当前日期的前七天

 var myDate = new Date(); //获取今天日期
 myDate.setDate(myDate.getDate() - 7);
 var dateArray = [];
 var dateTemp;
 var flag = 1;
 for (var i = 0; i < 7; i++) {
     dateTemp = (myDate.getMonth()+1)+"-"+myDate.getDate();
     dateArray.push(dateTemp);
     myDate.setDate(myDate.getDate() + flag);
 }

2.获取系统当前时间

 var nowdate = new Date();
 var y = nowdate.getFullYear();
 var m = nowdate.getMonth()+1;
 var d = nowdate.getDate();
 var formatnowdate = y+'-'+m+'-'+d;
 alert(formatnowdate);

3.获取系统前一周的时间

 var oneweekdate = new Date(nowdate-7*24*3600*1000);
 var y = oneweekdate.getFullYear();
 var m = oneweekdate.getMonth()+1;
 var d = oneweekdate.getDate();
 var formatwdate = y+'-'+m+'-'+d;
 alert(formatwdate);

4.获取系统前一个月的时间

 nowdate.setMonth(nowdate.getMonth()-1);
 var y = nowdate.getFullYear();
 var m = nowdate.getMonth()+1;
 var d = nowdate.getDate();
 var formatwdate = y+'-'+m+'-'+d;
 alert(formatwdate);

5.获取当前时间

 var date = new 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();
 alert(year + '-' + month + '-' + day  + ' ' + hour + ':' + minute + ':' + second);

6.获取一星期前的时间

 var now = new Date();
 var date = new Date(now.getTime() - 7 * 24 * 3600 * 1000);
 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();
 alert(year + '-' + month + '-' + day  + ' ' + hour + ':' + minute + ':' + second);

Q.E.D.


学而不厌 不耻下问