1、比较时间
//比较时间,格式yyyy-MM-dd //小于0前者大于后者,大于0前者小于后者,等于0两者相等 function CompareTime(startTime, endTime) { var startArr = startTime.split("-"); var starttime = new Date(startArr[0], startArr[1], startArr[2]); var starttimes = starttime.getTime(); var endArr = endTime.split("-"); var endtime = new Date(endArr[0], endArr[1], endArr[2]); var endtimes = endtime.getTime(); if (starttimes > endtimes) { return -1; } else if (starttimes == endtimes) { return 0; } else { return 1; } }
//日期格式yyyy-mm-dd HH:mm:ss(未验证整理) function comptime() { var beginTime = "2009-09-21 00:00:00"; var endTime = "2009-09-21 00:00:01"; var beginTimes = beginTime.substring(0, 10).split('-'); var endTimes = endTime.substring(0, 10).split('-'); beginTime = beginTimes[1] + '-' + beginTimes[2] + '-' + beginTimes[0] + ' ' + beginTime.substring(10, 19); endTime = endTimes[1] + '-' + endTimes[2] + '-' + endTimes[0] + ' ' + endTime.substring(10, 19); alert(beginTime + "aaa" + endTime); alert(Date.parse(endTime)); alert(Date.parse(beginTime)); var a = (Date.parse(endTime) - Date.parse(beginTime)) / 3600 / 1000; if (a < 0) { alert("endTime小!"); } else if (a > 0) { alert("endTime大!"); } else if (a == 0) { alert("时间相等!"); } else { return 'exception' } }