ここでのデモ:http://jsfiddle.net/2h7Sj/
@Palashからロジックの一部を借用しました(ただし、バグを修正しましたgetMonday
)。getSunday
このソリューションでは、特定の日付を選択し、日付範囲を選択できます。
HTML
<input type="text" id="dateRange" />
<input type="hidden" id="hiddenDate" />
JavaScript
$('#dateRange').datepicker({
altField: '#hiddenDate',
firstDay: 1,
onSelect: function(dateText, dateRange) {
var dateRange = $(dateRange);
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
hiddenDate = $('#hiddenDate'),
theDate = new Date(dateText),
dayOfTheWeek = theDate.getDay(),
mondayOffset = theDate.getDate() - dayOfTheWeek + (dayOfTheWeek == 0 ? -6 : 1),
sundayOffset = theDate.getDate() - dayOfTheWeek + (dayOfTheWeek == 0 ? 0 : 7),
monday = new Date(theDate.setDate(mondayOffset)),
sunday = new Date(theDate.setDate(sundayOffset)),
mondayText = months[monday.getMonth()] + '. ' + monday.getDate(),
sundayText = months[sunday.getMonth()] + '. ' + sunday.getDate(),
rangeText = mondayText + ' - ' + sundayText;
$(this).val(rangeText);
$(dateRange).data('rangetext', rangeText);
},
beforeShow: function(dateRange) {
var hiddenDate = $('#hiddenDate');
$(dateRange).val(hiddenDate.val());
},
onClose: function(selectedDateText, dateRange) {
dateRange = $(dateRange);
if($(this).val() == '') {
$('#hiddenDate').val('');
} else {
if(selectedDateText.indexOf('-') < 0 && dateRange.data('rangetext')) {
console.log('data-rangetext', dateRange.data('rangetext'));
$(this).val(dateRange.data('rangetext'));
}
}
}
});