0

通过拖动 html 表格单元格,我变得像 startTime 13:30:00 endTime 14:15:00(24 小时格式)。在隐藏字段中,我存储值 starttime 和 endtime

                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;
            }
4

2 回答 2

2

假设日期将是今天,您可以创建包含开始和结束时间的 Date 对象,然后将它们与当前日期进行比较,就像这样。

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.");
}
于 2012-07-05T08:21:27.000 回答
0

您可以像这样比较 2 个 Date 对象:

// 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() 可以理解的格式

于 2012-07-05T08:12:49.700 回答