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

【转】js获取当前指定的前几天的日期(如当前时间的前七天的日期)

时间:2015-05-21 19:13:25      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

 1 <html>
 2  <head> 
 3   <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
 4   <meta http-equiv="Content-Language" content="zh-cn" /> 
 5   <title>时间</title> 
 6  </head> 
 7  <body>   
 8   <script>
 9 function getBeforeDate(n){
10     var n = n;
11     var d = new Date();
12     var year = d.getFullYear();
13     var mon=d.getMonth()+1;
14     var day=d.getDate();
15     if(day <= n){
16             if(mon>1) {
17                mon=mon-1;
18             }
19            else {
20              year = year-1;
21              mon = 12;
22              }
23            }
24           d.setDate(d.getDate()-n);
25           year = d.getFullYear();
26           mon=d.getMonth()+1;
27           day=d.getDate();
28      s = year+"-"+(mon<10?(0+mon):mon)+"-"+(day<10?(0+day):day);
29      return s;
30 }
31 console.log(getBeforeDate(1));//昨天的日期
32 console.log(getBeforeDate(7));//前七天的日期
33 </script>
34  </body>
35 </html>

 

【转】js获取当前指定的前几天的日期(如当前时间的前七天的日期)

标签:

原文地址:http://www.cnblogs.com/warmth/p/4520525.html

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