js获取时间

来源:互联网 发布:淘宝企业店铺运营 编辑:程序博客网 时间:2024/06/05 20:40

<script type="text/javascript">
  var Dates = {};
  Dates.box = function(){
   var n,d,nowdate,otherdate,style;
   return{
    date:function(){
     //获取当日年、当月、当日时间
     d = new Date();
     nowdate = {ny:d.getFullYear(),nm:d.getMonth()+1,nd:d.getDate()};
     return this.dateGroup(nowdate,style);
    },
    sdate:function(){
     //获取第N天的日期
     d.setTime(d.getTime()+24*(n-1)*3600*1000);
     otherdate = {ny:d.getFullYear(),nm:d.getMonth()+1,nd:d.getDate()};
     return this.dateGroup(otherdate,style);
    },
    dateGroup:function(y,style){
     //日期拼合
     switch(style){
      case 1:
       ndate = y.ny+'-'+y.nm+'-'+y.nd;
       break;
      case 2:
       ndate = y.nm+'-'+y.nd+'-'+y.ny;
       break;
     }
     return ndate
    }
    ,
    showDate:function(a,b){
     n = a;
     style = b;
     //展示时间
     nowdate = this.date();
     otherdate = this.sdate();
    }
    
    
   }
  }();
  Dates.box.showDate(5,1);
  
    </script>

写这个日期时间,学到 d.setTime、d.getTime()

d.getTime()是获取当前天数的时间,单位是毫米(所以计算一天的方法是24*3600*1000)一天有24小时,一小时3600秒,因为这是以毫秒记所以乘以1000即为1天的毫秒时。而setTime是设置当前时间的。

d.setTime(d.getTime()+24*(n-1)*3600*1000);  //n-1其实就是算几天的,比如2-1到2-5,这五天,那么n就是5。


原创粉丝点击