js 获取时间差

时间:2022-01-28 18:00:32

写这片博客 ,下面代码虽然简单,但却很实用...默默留下来...

var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var halfamonth = day * 15;
var month = day * 30;
function Diff(type,dateNew,dateOld){
    var time=dateNew.getTime()-dateOld.getTime();
    if(time<0)
        return;
    var newValue="";
   switch (type){
       case "minute":
           newValue=time/minute;
           break;
       case "hour":
           newValue=time/hour;
           break;
       case "day":
           newValue=time/day;
           break;
       case "halfamonth":
           newValue=time/halfamonth;
           break;
       case "month":
           newValue=time/month;
           break;
   }
    return newValue;
}
//console.log(unique(o))
    var s="a,b,b";
    var t= s.split(",");
    console.log(t);
    console.log(contains("d",t) );
   var datebefore=new Date();
    var dateafter=new Date();
    var t=dateafter-datebefore;
    // alert(t+"s");
    var  s="2014-10-13 15:09:18";
    var dateOld=new Date(s);
    var dateNew=new Date("2014-10-15 15:09:18")    ;
    console.log(Diff("day",dateNew,dateOld));