0
我有一個表單(使用JavaScript),其中用戶應該輸入信息,如他們的工作開始時間,中斷開始時間,中斷結束時間和工作結束時間。我有4個文本框用於此目的。輸入到文本框中的時間爲hhmm
格式(僅限24小時)。時間驗證(Javascript)
要求:
1. The break times (start and end) must be within the work start and end time.
2. Break start must be before break end time.
3. Users can take up work shifts during the day or even overnight.
4. Work time(excluding breaks) should be less than 8 hours and greater than 4 hours.
因此,一些典型的有效條目是:
User1-
Start time: 0900
Break start time:1300
Break end time:1400
End time:1600
User2-
Start time:2200
Break start time:2300
Break end time:2330
End time:0400
一些無效的條目是:
Case1 - Break start before break end
Start time:2200
Break start time: 2330
Break end time: 2300
End time:0400
Case 2 -Breaks outside work time
Start time:2200
Break start time:1830
Break end time: 1900
End time:0400
我無法寫驗證代碼(JavaScript)的要求1,2 & 3.請幫助我。
這是我到目前爲止。 (請注意:我不能使用Date對象。)
var wrkSt = getElement('TB1').value;
var wrkSt_hr = parseFloat(wrkSt.substr(0,2));
var wrkSt_mn= parseFloat(wrkEd.substr(2,2));
var brkSt = getElement('TB2').value;
var brkSt_hr = parseFloat(brkSt.substr(0,2));
var brkSt_mn= parseFloat(brkEd.substr(2,2));
var brkEd = getElement('TB3').value;
var brkEd_hr = parseFloat(brkSt.substr(0,2));
var brkEd_mn= parseFloat(brkEd.substr(2,2));
var wrkEd = getElement('TB4').value;
var wrkEd_hr = parseFloat(wrkEd.substr(0,2));
var wrkEd_mn= parseFloat(wrkEd.substr(2,2));
var msg='';
if ((wrkSt_hr > wrkEd_hr) || ((wrkSt_hr == wrkEd_hr) && (wrkSt_mn >= wrkEd_mn)))
{
msg+='shift overnight selected';
}
if (wrkEd_hr>12){wrkEd_hr-=12;}
if (wrkSt_hr >12){wrkSt_hr -=12;}
if (brkSt_hr>12){brkSt_hr-=12;}
if (brkEd_hr>12){brkEd_hr-=12;}
var Breakdiff = ((brkEd_hr - brkSt_hr)*60) + (brkEd_mn - brkSt_mn);
var Workdiff_tot = ((wrkEd_hr- wrkSt_hr)*60) + (wrkEd_mn -wrkSt_mn);
var Shiftdiff = Workdiff_tot - Breakdiff;
if (Shiftdiff > 480) //8hours = 8*60 = 480 min
{
msg+='Time greater than 8 hrs';
}
if (Shiftdiff < 240) //4 hours = 4*60 = 240 min
{
msg+='Time less than 4 hrs';
}
請幫我檢查休息和工作時間的邏輯。 Thx提前提供任何幫助!
如果這些時間跨越一天以上,您需要同時擁有日期和時間。 – Dutchie432 2011-05-04 17:41:45
@Dutchie:這可能看起來很奇怪,但日期並不適用於我的代碼。因此,如果用戶輸入2200到0400(10:00p到04:00a),則只使用時間,因此它與日期無關。 Thx無論如何:) – uno 2011-05-04 17:45:39
然後,如果小時是00-04,你需要添加24它使它超過2300 – Dutchie432 2011-05-04 17:50:16