2014-05-01 45 views
0

reportdate_paystartdate是我的jQuery函數中的兩個日期變量,其中_paystartdate是付款期限的起始日期,reportdate是單日期的日期。現在我需要檢查reportdate是否在payperiod的第一週,這意味着reportdate應該在_paystartdate_paystartdate+6之間。我如何實現這一目標?如何使用jquery檢查某個特定日期是否在一週內?

var _paystartdate = $('[id$=hdnPayPeriodStartDt]').val(); 
var reportdate = $('[id$=datepicker]').val(); 
+0

對它們進行比較。如果您有複雜的日期/時間操作,也許你應該考慮使用[moment.js](http://momentjs.com)。檢查[示例答案](http://stackoverflow.com/questions/17219623/moment-js-only-within-the-last-week)。 – acdcjunior

回答

0

您不需要使用jQuery來執行此比較。只是純粹的JavaScript。

如果我們開始與您的代碼

var _paystartdate = $('[id$=hdnPayPeriodStartDt]').val(); 
var reportdate = $('[id$=datepicker]').val(); 

然後,我們需要轉換爲日期類型,像這樣:

var payStartDate = new Date(_paystartdate); 
var reportDate = new Date(reportdate); 
var payEndDate = payStartDate.setDate(payStartDate.getDate()+6); //payEndDate should now been 6 days after payStartDate 

現在我們可以適當地

if (reportDate >= payStartDate && reportDate <= payEndDate) { 
    //report date is between pay start date and pay end date 
} 
+0

非常感謝,我會試試這個。我希望這應該工作。我在表格中有另一欄可以顯示幾小時。現在我應該只添加特定周的小時數.var _paystartdate = $('[id $ = txtHours]')。val();請幫幫我。如果我的問題不清楚,請告訴我。 – user3154990

+0

txtHours會去什麼? paystartdate和/或reportdate? – attila

+0

它在本週每天的價值。我有多個小時的行,但我只需要總結payperiodstartdate和payperiodstart + 6之間的小時數值,這意味着第一週的payperiod。一般payperiod是雙週 – user3154990

相關問題