JavaScript计算倒计时

/**
 * 计算倒计时
 * @param endtime('yyyy-mm-dd hh:mm:ss' 格式 或者 timestamp 格式)
 * @param 'yyyy-mm-dd hh:mm:ss' 格式 或者 timestamp 格式)
 * @return obj对象(obj.days(天),obj.hours(时),obj.minutes(分),obj.seconds(秒))
 */
function caculateCountDownTime(endtime,nowtime){
    var timeObj = new Object();
    var endtimeDate = new Date(endtime);
    var nowtimeDate = new Date(nowtime);
    var endtimeStamp;
    var nowtimeStamp;
    if(endtimeDate == "Invalid Date"){
        endtimeStamp = endtime;
    }else{
        endtimeStamp = endtimeDate.getTime();
    }
    if(nowtimeDate == "Invalid Date"){
        nowtimestamp = nowtime;
    }else{
        nowtimestamp = nowtimeDate.getTime();
    }
    if(endtimeStamp>nowtimestamp){
          var date3=endtimeStamp-nowtimestamp; //时间差的毫秒数
          //计算出相差天数
          var days=Math.floor(date3/(24*3600*1000))
          //计算出小时数
          var leave1=date3%(24*3600*1000)    //计算天数后剩余的毫秒数
          var hours=Math.floor(leave1/(3600*1000))
          //计算相差分钟数
          var leave2=leave1%(3600*1000)        //计算小时数后剩余的毫秒数
          var minutes=Math.floor(leave2/(60*1000))
          //计算相差秒数
          var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
          var seconds=Math.round(leave3/1000)
          timeObj.days = days;
          timeObj.hours = hours;
          timeObj.minutes = minutes;
          timeObj.seconds = seconds;
      }else{
          timeObj.days = 0;
          timeObj.hours = 0;
          timeObj.minutes = 0;
          timeObj.seconds = 0;
      }
      return timeObj;
}

分享名称:JavaScript计算倒计时
标题路径:http://ybzwz.com/article/jsjsdd.html