js计算某个时间距离当前时间的日时秒

 //By Arliang
  //23:07 2010-2-1
  //百度知道http://z.baidu.com/question/135976703.html
  var $ = document.getElementById;
  var intervalID = 0;
  function calculate(inputTime){
    var now = new Date();
    var t = new Date(inputTime);
    var past = (inputTime < now.getTime()) ? 1 : 0; //输入的时间过去了就是1,否则为0
    var difference = 0; //差值
    //下面的差值减去八个小时,是因为new Date(0)是"Thu Jan 1 08:00:00 UTC+0800 1970",从八点开始算的
    if(past) difference = new Date(now.getTime() - inputTime - 8 * 3600 * 1000);
    else difference = new Date(inputTime - now.getTime() - 8 * 3600 * 1000);
    
    //计算过去的天数、小时、分钟和秒。天数要自己算,其它的get就行了
    var dDays = parseInt(difference.getTime() / 3600 / 24 /1000);
    var dHours = difference.getHours();
    var dMinutes =difference.getMinutes();
    var dSeconds = difference.getSeconds();
    var str=new Array();
    str[0]=dDays;
    str[1]=dHours;
    str[2]=dMinutes;
    str[3]=dSeconds;
    return str;
  
  }
  
  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章