javascript 日期比较
自己写的一个函数,可以对混合的日期格式进行比较如“2008年9月23日12时36分4秒”和"2008-9-23 13:37:4"。
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script>
//这里是测试代码
var start = 0;
var end = 0;
start = new Date().getTime();
var a = "2008年02-03日20时36分4秒"
var b = "2008-02-03 20:36:4"
document.write("比较结果为:"+comparDate(b,a)+"<br>")
end = new Date().getTime();
document.write("耗时:"+(end - start) / 1000 + "sec");
//测试代码结束
function comparDate(date1, date2) {
var patton1 = /[日秒]/g;
var patton2 = /[年月-]/g;
var patton3 = /[时分:]/g;
date1 = date1.replace(patton1," ").replace(patton2,"/").replace(patton3,":");
date2 = date2.replace(patton1," ").replace(patton2,"/").replace(patton3,":");
document.write(date1 + "<br>");
document.write(date2 + "<br>");
var Temp = (Date.parse(date1) - Date.parse(date2)); // 24*60*60*1000
var result = Temp > 0 ? 1 : Temp == 0 ? 0 : -1;
return result;//返回1是大于,返回0是等于,返回-1是小于
}
</script>
(function($){
})(jQuery);