js st转date, 时间比较及加减

function  DateDiff(sDate1,  sDate2){    //sDate1和sDate2是 s1  =  "2006-12-18"  s2  =  "2007-01-05" 
       var  aDate,  oDate1,  oDate2,  iDays  
       aDate  =  sDate1.split("-")  
       oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])    //转换为12-18-2006格式  
       aDate  =  sDate2.split("-")  
       oDate2  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])  
       iDays  =  parseInt((oDate1  -  oDate2)  /  1000  /  60  /  60  /24)    //把相差的毫秒数转换为天数  
       return  iDays  
   }    

 var  str="2008-04-02 10:08:44";
       str =  str.replace(/-/g,"/");
      //// str =  str.replace("T"," "); 
   //

//因之前在数据库中的类型为2008-04-02 10:08:44,必须转化为2008/04/02 10:08:44格式才能实例化Date对象

var a = new Date(str); var b= new Date(); var aTimes = a.getTime(); var bTimes = b.getTime(); if (aTimes>bTimes) { alert("aTimes>bTimes"); } else if (aTimes<bTimes) { alert("aTimes<bTimes"); } else { alert("aTimes=bTimes"); } alert(DateDiff("2007-01-05","2006-12-18"));

 

你可能感兴趣的:(Date)