0

HTMLテーブルのセルをドラッグしてstartTime13:30:00endTime 14:15:00(24時間形式)のようになります。隠しフィールドに開始時間と終了時間の値を保存しています

                var hidStartTime=13:30:00;
                var hidEndTime=14:15:00;

ユーザーのスタータイムとエンドタイムが現在のクライアントマシンの時刻と比較して過ぎているかどうかを確認してから、alert( "過去の時刻の予定を修正することはできません。");を生成する必要があります。確認方法

            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 に答える