javascript日期比较

1.比较日期大小
<script>
function CompareDate(d1,d2)
{
   return ((new Date(d1.replace(/-/g,"//"))) > (new Date(d2.replace(/-/g,"//"))));
}
var current_time = "2007-02-02 7:30";
var stop_time = "2007-01-31 8:30";
alert(CompareDate(current_time,stop_time));
</script>


2.两个日期相差天数
<SCRIPT LANGUAGE="JavaScript">
<!--
/*
   日期格式2008-01-20
*/
function DateDiff(sDate1,sDate2){
   var aDate,oDate1,oDate2,iDays ;
   aDate =sDate1.split('-'); 
   oDate1 = new Date(aDate[1]+'-'+aDate[2]+'-'+aDate[0]) ;
   //转换为04-19-2007格式 
   aDate = sDate2.split('-'); 
   oDate2 = new Date(aDate[1]+'-'+ aDate[2] +'-'+aDate[0]); 
   iDays = parseInt(Math.abs(oDate1 -oDate2)/1000/60/60/24);//把相差的毫秒数转换为天数 
   return iDays ;
   }
   alert(DateDiff('2008-02-25','2008-03-02'));
//-->
</SCRIPT>

posted @ 2016-11-28 13:56  To哥  阅读(246)  评论(0编辑  收藏  举报