2017-06-12 150 views
0

如何更改此每週jQuery日期選擇範圍以星期一開始周?JQuery Datepicker - 從週一開始選擇每週範圍

http://jsfiddle.net/oykmv300/

$(function() { 
var startDate; 
var endDate; 

var selectCurrentWeek = function() { 
    window.setTimeout(function() { 
     $('.week-picker').find('.ui-datepicker-current-day a').addClass('ui-state-active') 
    }, 1); 
} 

$('.week-picker').datepicker({ 
    showOtherMonths: true, 
    selectOtherMonths: true, 
    firstDay: 1, 
    onSelect: function(dateText, inst) { 
     var date = $(this).datepicker('getDate'); 
     startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay()); 
     endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6); 
     var dateFormat = inst.settings.dateFormat || $.datepicker._defaults.dateFormat; 
     $('#startDate').text($.datepicker.formatDate(dateFormat, startDate, inst.settings)); 
     $('#endDate').text($.datepicker.formatDate(dateFormat, endDate, inst.settings)); 

     selectCurrentWeek(); 
    }, 
    beforeShowDay: function(date) { 
     var cssClass = ''; 
     if(date >= startDate && date <= endDate) 
      cssClass = 'ui-datepicker-current-day'; 
     return [true, cssClass]; 
    }, 
    onChangeMonthYear: function(year, month, inst) { 
     selectCurrentWeek(); 
    } 
}); 

$('.week-picker .ui-datepicker-calendar tr').live('mousemove', function() { $(this).find('td a').addClass('ui-state-hover'); }); 
$('.week-picker .ui-datepicker-calendar tr').live('mouseleave', function() { $(this).find('td a').removeClass('ui-state-hover'); }); 
}); 

我改變日期選擇器開始的第一天到firstDay:1它與視覺上週一開始。但是,如何在週一開始時選擇每週更改每週範圍?

+0

看到這個http://jsfiddle.net/oykmv300/1/ – Amal

+0

完美。萬分感謝! –

回答

2

試試這個代碼

重置您的startend日期

startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 1); 
endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 7); 

DEMO