欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js前台判断开始时间是否小于结束时间

程序员文章站 2023-10-28 19:56:46
代码如下: // 判断开始时间是否小于结束时间 if ($("#txtbegintime").length > 0 && $("#txtendti...

代码如下:

// 判断开始时间是否小于结束时间
if ($("#txtbegintime").length > 0 && $("#txtendtime").length > 0) {
if (date.parse(timeformattosql($("#txtbegintime").val()).replace("-", "/")) > date.parse(timeformattosql($("#txtendtime").val()).replace("-", "/"))) {
alert("开始时间不能大于结束时间!");
// $("#txtbeginsearchtimebybe").focus();
return;
///<summary>/// 将传递的时间值转换为sql识别的时间格式
///<param name="strtime">时间(正常的页面显示时间格式)</param>
///</summary>
function timeformattosql(strtime) {
var strresult = "";
var strtemp = "";
for (var i = 0; i < strtime.length; i++) {
strtemp = strtime.substr(i, 1);
if (strtemp == "年" || strtemp == "月")
strresult += "-";
else
if (strtemp == "日" || strtemp == "秒") {
if (strtemp == "日")
strresult += "|";
else
strresult += "";
}
else
if (strtemp == "时" || strtemp == "分")
strresult += ":";
else
strresult += strtemp;
}
var strargdatetime = strresult.split('|'); //此时的时间格式可能为2010-11-11 11: 或2010-11-11 11等格式
if (strargdatetime.length == 1) {
//日期部分进行处理
var strargdate = strargdatetime[0].split('-'); //此时对时间部分进行处理,可能为11: 11 或11:00等格式
if (strargdate.length == 2) {
if (strargdate[1].length < 1)
strresult = strargdate[0];
else
strresult = strargdatetime[0] + "-01";
} else
if (strargdate.length == 3) {
if (strargdate[2].length < 1)
strresult = strargdate[0] + "-" + strargdate[1] + "-01";
}
}
else
if (strargdatetime.length == 2) {
//时间部分进行处理
var strargtime = strargdatetime[1].split(':'); //此时对时间部分进行处理,可能为11: 11 或11:00等格式
if (strargtime.length == 1) {
strresult = strargdatetime[0] + " " + strargdatetime[1] + ":00:00"
} else
if (strargtime.length == 2) {
if (strargtime[1].length < 1)
strresult = strargdatetime[0] + " " + strargdatetime[1] + "00"
else
strresult = strargdatetime[0] + " " + strargdatetime[1] + ":00"
} else
if (strargtime.length == 3) {
if (strargtime[2].length < 1)
strresult = strargdatetime[0] + " " + strargdatetime[1] + "00"
}
}
return strresult;
}