码迷,mamicode.com
首页 > 编程语言 > 详细

javascript如何计算两个日期之间的时间间隔

时间:2016-01-04 22:24:30      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

javascript如何计算两个日期之间的时间间隔:
有时候我们需要获取两个日期之间的时间间隔,下面是一段比较常用且兼容所有浏览器的代码,希望能够需要的带来一定帮助。
代码如下:

 

function NewDate(str)
{ 
  str=str.split(‘-‘); 
  var date=new Date(); 
  date.setUTCFullYear(str[0], str[1] - 1, str[2]); 
  date.setUTCHours(0, 0, 0, 0); 
  return date; 
} 
function TimeCom(dateValue) 
{ 
  var newCom; 
  if (dateValue == "") 
  { 
    newCom = new Date(); 
  } 
  else 
  { 
    newCom = NewDate(dateValue); 
  } 
  this.year = newCom.getYear(); 
  this.month = newCom.getMonth() + 1; 
  this.day = newCom.getDate(); 
  this.hour = newCom.getHours(); 
  this.minute = newCom.getMinutes(); 
  this.second = newCom.getSeconds(); 
  this.msecond = newCom.getMilliseconds(); 
  this.week = newCom.getDay(); 
} 
function DateDiff(interval, date1, date2) 
{ 
  var TimeCom1 = new TimeCom(date1); 
  var TimeCom2 = new TimeCom(date2); 
  var result; 
  switch (String(interval).toLowerCase()) 
  { 
    case "y": 
    case "year": 
    result = TimeCom1.year - TimeCom2.year; 
    break; 
    case "m": 
    case "month": 
    result = (TimeCom1.year - TimeCom2.year) * 12 + (TimeCom1.month - TimeCom2.month); 
    break; 
    case "d": 
    case "day": 
    result = Math.round((Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day)) / (1000 * 60 * 60 * 24)); 
    break; 
    case "h": 
    case "hour": 
    result = Math.round((Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day, TimeCom1.hour) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day, TimeCom2.hour)) / (1000 * 60 * 60)); 
    break; 
    case "min": 
    case "minute":result = Math.round((Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day, TimeCom1.hour, TimeCom1.minute) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day, TimeCom2.hour, TimeCom2.minute)) / (1000 * 60)); 
    break; 
    case "s": 
    case "second":result = Math.round((Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day, TimeCom1.hour, TimeCom1.minute, TimeCom1.second) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day, TimeCom2.hour, TimeCom2.minute, TimeCom2.second)) / 1000); 
    break; 
    case "ms": 
    case "msecond": result = Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day, TimeCom1.hour, TimeCom1.minute, TimeCom1.second, TimeCom1.msecond) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day, TimeCom2.hour, TimeCom2.minute, TimeCom2.second, TimeCom1.msecond); 
    break; 
    case "w": 
    case "week":result = Math.round((Date.UTC(TimeCom1.year, TimeCom1.month - 1, TimeCom1.day) - Date.UTC(TimeCom2.year, TimeCom2.month - 1, TimeCom2.day)) / (1000 * 60 * 60 * 24)) % 7; 
    break; 
    default: 
    result = "invalid"; 
  } 
  return (result); 
}

 

原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=9046

更多内容可以参阅:http://www.softwhy.com/javascript/

 

javascript如何计算两个日期之间的时间间隔

标签:

原文地址:http://www.cnblogs.com/come-on/p/5100336.html

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