标签:
//适合格式(0000-00-00)
//日期比较 function dateCompare(startdata, enddata) { var arr = startdata.split("-"); var starttime = new Date(arr[0], arr[1], arr[2]); var starttimes = starttime.getTime(); var arrs = enddata.split("-"); var lktime = new Date(arrs[0], arrs[1], arrs[2]); var lktimes = lktime.getTime(); if (starttimes > lktimes) { return false; } else return true; }
//调用 var startdata = $("#ipt_startdate").datebox(‘getValue‘); var enddata = $("#ipt_enddate").datebox(‘getValue‘); if (!dateCompare(startdata, enddata)) { return $.messager.alert(‘提示‘, ‘您输入的时间有误!‘, ‘warning‘); }
JS时间比较 //比较日期大小 function compareDate(checkStartDate, checkEndDate) { var arys1 = new Array(); var arys2 = new Array(); if (checkStartDate != null && checkEndDate != null) { arys1 = checkStartDate.split(‘-‘); var sdate = new Date(arys1[0], parseInt(arys1[1] - 1), arys1[2]); arys2 = checkEndDate.split(‘-‘); var edate = new Date(arys2[0], parseInt(arys2[1] - 1), arys2[2]); if (sdate > edate) { // alert("日期开始时间大于结束时间"); return true; } else { //alert("通过"); return false; } } } //判断日期,时间大小 function compareTime(startDate, endDate) { if (startDate.length > 0 && endDate.length > 0) { var startDateTemp = startDate.split(" "); var endDateTemp = endDate.split(" "); var arrStartDate = startDateTemp[0].split("-"); var arrEndDate = endDateTemp[0].split("-"); var arrStartTime = startDateTemp[1].split(":"); var arrEndTime = endDateTemp[1].split(":"); var allStartDate = new Date(arrStartDate[0], arrStartDate[1], arrStartDate[2], arrStartTime[0], arrStartTime[1], arrStartTime[2]); var allEndDate = new Date(arrEndDate[0], arrEndDate[1], arrEndDate[2], arrEndTime[0], arrEndTime[1], arrEndTime[2]); if (allStartDate.getTime() >= allEndDate.getTime()) { // alert("startTime不能大于endTime,不能通过"); return true; } else { //alert("startTime小于endTime,所以通过了"); return false; } } else { alert("时间不能为空"); return true; } } //比较日期,时间大小 function compareDateTime(startDate, endDate) { if (startDate.indexOf(" ") != -1 && endDate.indexOf(" ") != -1) { //包含时间,日期 return compareTime(startDate, endDate); } else { //不包含时间,只包含日期 return compareDate(startDate, endDate); } }
js日期比较(yyyy-mm-dd) function duibi(a, b) { var arr = a.split("-"); var starttime = new Date(arr[0], arr[1], arr[2]); var starttimes = starttime.getTime(); var arrs = b.split("-"); var lktime = new Date(arrs[0], arrs[1], arrs[2]); var lktimes = lktime.getTime(); if (starttimes >= lktimes) { alert(‘开始时间大于离开时间,请检查‘); return false; } else return true; } js时间比较(yyyy-mm-dd hh:mi: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‘ } }
标签:
原文地址:http://www.cnblogs.com/ooip/p/4898243.html