Javascript 如何检查失效时间
通过在html表格单元格上拖动,我变得像startTime 13:30:00 endTime 14:15:00(24小时格式)。 在hiddenfield中,我存储starttime和endtime值Javascript 如何检查失效时间,javascript,jquery,asp.net,html,time,Javascript,Jquery,Asp.net,Html,Time,通过在html表格单元格上拖动,我变得像startTime 13:30:00 endTime 14:15:00(24小时格式)。 在hiddenfield中,我存储starttime和endtime值 var hidStartTime=13:30:00; var hidEndTime=14:15:00; 我必须检查用户的开始时间和结束时间是否超过当前客户端计算机时间,然后必须生成警报(“您不能允许为过去的时间修正约会”); 我怎么
var hidStartTime=13:30:00;
var hidEndTime=14:15:00;
我必须检查用户的开始时间和结束时间是否超过当前客户端计算机时间,然后必须生成警报(“您不能允许为过去的时间修正约会”); 我怎么检查
var todaysDate = new Date();
var currentHour = todaysDate.getHours();
var currentMinutes = todaysDate.getMinutes();
if (currentHour < endTimeHour)
{
alert("You can not allow to fix appointment for past time.");
return false;
}
var todaysDate=新日期();
var currentHour=todaysDate.getHours();
var currentMinutes=todaysDate.getMinutes();
如果(当前小时<结束小时)
{
警报(“您不能允许为过去的时间安排约会。”);
返回false;
}
您可以像这样比较两个日期对象:
// Get current date on the client machine
var currentDateTime = new Date(); // "Thu, 05 Jul 2012 08:05:57 GMT"
// Now if you have a date string of 8:30 on the same day
var endDateTime = Date.parse("Thu, 05 Jul 2012 08:30:00 GMT");
if (currentDateTime > endDateTime) {
alert("...");
}
困难的部分可能是将日期字符串组合成函数date.parse()可以理解的格式假定日期是今天,您可以创建具有开始和结束时间的日期对象,然后将它们与当前日期进行比较,如下所示
var currDate = new Date();
var startDate = setTime(hidStartTime);
var endDate = setTime(hidEndTime);
// given an input string of format "hh:mm:ss", returns a date object with
// the same day as today, but the given time.
function setTime(timeStr) {
var dateObj = new Date(); // assuming date is today
var timeArr = timeStr.split(':'); // to access hour/minute/second
var hour = timeArr[0];
var minute = timeArr[1];
var second = timeArr[2];
dateObj.setHours(hour);
dateObj.setMinutes(minute);
dateObj.setSeconds(second);
return dateObj;
}
// now we can subtract them (subtracting two Date objects gives you their
// difference in milliseconds)
if (currDate - startDate < 0 || currDate - endDate < 0) {
alert("Unfortunately, you can't schedule a meeting in the past.
We apologize for the inconvenience.");
}
var currDate=新日期();
var startDate=设置时间(hidStartTime);
var endDate=设置时间(hidEndTime);
//给定格式为“hh:mm:ss”的输入字符串,返回带有
//今天是同一天,但时间有限。
函数设置时间(timeStr){
var dateObj=new Date();//假设日期是今天
var timeArr=timeStr.split(“:”);//访问时/分/秒
var小时=timeArr[0];
var minute=timeArr[1];
var second=timeArr[2];
dateObj.设定小时数(小时);
dateObj.setMinutes(分钟);
dateObj.setSeconds(秒);
返回日期OBJ;
}
//现在我们可以减去它们(减去两个日期对象会得到它们的
//差异(以毫秒为单位)
如果(当前日期-开始日期<0 | |当前日期-结束日期<0){
警报(“很遗憾,您无法在过去安排会议。
对于给您带来的不便,我们深表歉意。”);
}
我必须检查用户的开始时间和结束时间是否超过当前客户端机器时间,然后必须生成警报“”;kayo:hav要比较这个时间var hidStartTime=13:30:00;var hidEndTime=14:15:00;使用当前机器时间OK。看来阿克尔给出了正确的答案。另外请注意,如果有人从不同的时区访问网站,则不会给出正确的结果。如果隐藏字段包含时区信息和绝对时间(如“Thu,2012年7月5日08:30:00 GMT”),那就更好了。服务器端应该在achal answer var startDate=setTime(hidStartTime)中生成这样一个string.kayo;var endDate=设置时间(hidEndTime);我今天得到的开始日期和结束日期date@John:不要使用var hidStartTime=13:30:00,而是尝试使用如下引号:var hidStartTime=“13:30:00”我尝试了var time1=setTime(“13:30:00”);var time2=设定时间(“13:31:00”);您可以通过console.log(time1.toString())和console.log(time2.toString())来检查它是否正常工作-两者都有不同的时间值。看起来是对的。然后您可以测试(time1Achal我必须只检查失效时间而不是日期
@John,因为我们使用Javascript的date
对象进行比较,即使您只对时间感兴趣,日期(dd-mm-yyyy)也需要设置为任何值,只要它们相等。而不是var-hidStartTime=“13:30:00”如果您可以在hidStartTime中获得日期时间字符串,如:var hidStartTime=“Thu,2012年7月5日15:20:06 GMT”,这将很好。如果这是可能的,它将简化问题并使其更可靠,因为您有时区信息。如果日期部分混乱,另一个解决方案是转换“13:30:00”“转换为秒,而不使用Javascript日期对象。示例:var hidStartTime=“13:30:00”;var arr=“13:30:00”。拆分(“:”);var hidStartSecs=arr[0]*3600+arr[1]*60+arr[2]*1;