首页 > 代码库 > js日期时间比较

js日期时间比较

 1 //js日期比较( 要求日期格式:yyyy-mm-dd) 2  3  function DateBiJiao(firstDate, lastDate) { 4     var arr = firstDate.split("-"); 5     var firsttime = new Date(arr[0], arr[1], arr[2]); 6     var firsttimes = firsttime.getTime(); 7  8     var arrs = lastDate.split("-"); 9     var  lasttime = new Date(arrs[0], arrs[1], arrs[2]);10     var  lasttimes =  lasttime.getTime();11 12     if (firsttimes>= lasttimes) {13         return false;14     }15     else16         return true;17 }18 19  20 21 //js时间比较( 要求日期格式:yyyy-mm-dd hh:mi:ss)22 23 function comptime(firstDate,lastDate) {24     var beginTime = firstDate;25     var endTime = lastDate;26     var beginTimes = beginTime.substring(0, 10).split(‘-‘);27     var endTimes = endTime.substring(0, 10).split(‘-‘);28 29     beginTime = beginTimes[1] + ‘-‘ + beginTimes[2] + ‘-‘ + beginTimes[0] + ‘ ‘ + beginTime.substring(10, 19);30     endTime = endTimes[1] + ‘-‘ + endTimes[2] + ‘-‘ + endTimes[0] + ‘ ‘ + endTime.substring(10, 19);31 32     alert(beginTime + "aaa" + endTime);33     alert(Date.parse(endTime));34     alert(Date.parse(beginTime));35     var a = (Date.parse(endTime) - Date.parse(beginTime)) / 3600 / 1000;36     if (a < 0) {37         alert("endTime小!");38     } else if (a > 0) {39         alert("endTime大!");40     } else if (a == 0) {41         alert("时间相等!");42     } else {43         return ‘exception‘44     }45 }

 

 

js日期时间比较