function check() { var startTime=$("#sTime").val(); var endTime=$("#eTime").val(); if(startTime==""){ alert("请输入开始时间!"); return; } if(endTime==""){ alert("请输入结束时间!"); return; } if(!checkEndTime(startTime,endTime)){ alert("结束时间必须晚于开始时间!"); return; } var sp=DateDiff(startTime,endTime); if(sp.Days>90) { alert("只能查询三个月内的数据!") return; } } function checkEndTime(startTime,endTime){ var start=new Date(startTime.replace("-", "/").replace("-", "/")); var end=new Date(endTime.replace("-", "/").replace("-", "/")); if(end<start){ return false; } return true; } function DateDiff(sDate1, sDate2){ //sDate1和sDate2是字符串 yyyy-MM-dd格式 var aDate, oDate1, oDate2, iDays, ihours, iminutes, iseconds; aDate = sDate1.split("-"); oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);//转换为MM-dd-yyyy格式 aDate = sDate2.split("-"); oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]); var timeSpan = {}; var TotalMilliseconds = Math.abs(oDate1 - oDate2);//相差的毫秒数 timeSpan.Days = parseInt(TotalMilliseconds / 1000 / 60 / 60 /24); timeSpan.TotalHours = parseInt(TotalMilliseconds / 1000 / 60 / 60); timeSpan.Hours = timeSpan.TotalHours % 24; timeSpan.TotalMinutes = parseInt(TotalMilliseconds / 1000 / 60); timeSpan.Minutes = timeSpan.TotalMinutes % 60; timeSpan.TotalSeconds = parseInt(TotalMilliseconds / 1000); timeSpan.Seconds = timeSpan.TotalSeconds % 60; timeSpan.TotalMilliseconds = TotalMilliseconds; timeSpan.Milliseconds = TotalMilliseconds % 1000; return timeSpan; }
获取当前时间
function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate; + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds(); return currentdate; }
对时间进行加减操作
var date = new Date(); date.setDate(date.getDate() + 60);//这里的60就是你要加的天数,减也可以。年、月会相应加上去,值得注意的是date.getMonth()得到的月份比实际月份小1,所以实际月份是(date.getMonth()+1) alert(date.getFullYear() +"-"+ (date.getMonth()+1) +"-"+ date.getDate());