前些天女友工作上需要一些根据天数以及小时算日期的东西,今天特地给她做出来,顺便留在这里,有希望能帮助到一些朋友

  1. <body>  
  2.   
  3. <form id="f1" name="form1" method="post" action="">  
  4.     按天算日期,注意12点之前的情况 <p>  
  5.   天数:<input type="text" id="t1" name="t1" onchange="addDay(this.value)" onpropertychange="addDay(this.value)"  />  
  6.    
  7.    
  8.   <p>  
  9.     结果:<input type="text" id="t2" name="t2" />  
  10.    
  11.   </p>  
  12. </form>  
  13. ------------------------------------------------------------------ 
  14.    
  15. <form id="f2" name="form2" method="post" action="">  
  16.     按小时算日期 <p>  
  17.    天数:<input type="text" id="t1" name="t1" onchange="addHours(this.value)" onpropertychange="addHours(this.value)"  />  
  18.    
  19.    
  20.   <p>  
  21.     结果:<input type="text" id="t2" name="t2" />  
  22.    
  23.   </p>  
  24. </form>  
  25. </body>  
  26. <script>  
  27.     function addHours(h){  
  28.         var now = new Date();  
  29.         now.addHours(parseInt(h));//加减日期操作  
  30.         f2.t2.value=now.Format("yyyy-MM-dd hh:mm:ss");  
  31.     }  
  32.     function addDay(d){  
  33.         var now = new Date();  
  34.         now.addDays(parseInt(d));//加减日期操作  
  35.         f1.t2.value=now.Format("yyyy-MM-dd hh:mm:ss");  
  36.     }  
  37.     Date.prototype.Format = function(fmt)   
  38.         {  
  39.             var o =  
  40.             {   
  41.                 "M+" : this.getMonth() + 1, //月份   
  42.                 "d+" : this.getDate(), //日   
  43.                 "h+" : this.getHours(), //小时   
  44.                 "m+" : this.getMinutes(), //分   
  45.                 "s+" : this.getSeconds(), //秒   
  46.                 "q+" : Math.floor((this.getMonth() + 3) / 3), //季度   
  47.                 "S" : this.getMilliseconds() //毫秒   
  48.             };   
  49.             if (/(y+)/.test(fmt))   
  50.                 fmtfmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));   
  51.             for (var k in o)   
  52.                 if (new RegExp("(" + k + ")").test(fmt))   
  53.                     fmtfmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));   
  54.             return fmt;   
  55.         }  
  56.         Date.prototype.addHours = function(h)  
  57.         {  
  58.             this.setHours(this.getHours() + h);  
  59.         };  
  60.           
  61.         Date.prototype.addDays = function(d)  
  62.         {  
  63.             this.setDate(this.getDate() + d);  
  64.         };  
  65.           
  66.           
  67.         Date.prototype.addWeeks = function(w)  
  68.         {  
  69.             this.addDays(w * 7);  
  70.         };  
  71.           
  72.           
  73.         Date.prototype.addMonths= function(m)  
  74.         {  
  75.             var d = this.getDate();  
  76.             this.setMonth(this.getMonth() + m);  
  77.           
  78.             if (this.getDate() < d)  
  79.                 this.setDate(0);  
  80.         };  
  81.           
  82.           
  83.         Date.prototype.addYears = function(y)  
  84.         {  
  85.             var m = this.getMonth();  
  86.             this.setFullYear(this.getFullYear() + y);  
  87.           
  88.             if (m < this.getMonth())   
  89.             {  
  90.                 this.setDate(0);  
  91.             }  
  92.         };  
  93. </script>