I am currently using bootstrap-datepicker (https://github.com/eternicode/bootstrap-datepicker), but want to be able to select entire weeks in the calendar (Monday to Sunday), as well as display the week interval in the input field that I am selecting from. The default interval in the input field should be the week that you are currently in.
I have seen a somewhat similar method using jQuery UI to display the interval on jsfiddle.
C
I have tried editing this code to work for bootstrap-datepicker, without any luck.
Any idea how I can implement this for bootstrap-datepicker? :
Any help is highly appreciated!
I have used Bootstrap datetime picker in my project ran into same problem like yours When trying to select the weeks.
Got the below Solution on my own you can try it.
Required Files :
HTML :
<div class="container">
<div class="row">
<div class="col-sm-6 form-group">
<div class="input-group" id="DateDemo">
<input type='text' id='weeklyDatePicker' placeholder="Select Week" />
</div>
</div>
</div>
</div>
JS : Used the moment.js for calculating the start and end of the week.
//Initialize the datePicker(I have taken format as mm-dd-yyyy, you can have your own)
$("#weeklyDatePicker").datetimepicker({
format: 'MM-DD-YYYY'
});
//Get the value of Start and End of Week
$('#weeklyDatePicker').on('dp.change', function (e) {
value = $("#weeklyDatePicker").val();
firstDate = moment(value, "MM-DD-YYYY").day(0).format("MM-DD-YYYY");
lastDate = moment(value, "MM-DD-YYYY").day(6).format("MM-DD-YYYY");
$("#weeklyDatePicker").val(firstDate + " - " + lastDate);
});
CSS :
.bootstrap-datetimepicker-widget tr:hover {
background-color: #808080;
}
Link to Working Code in JSFiddle :
https://jsfiddle.net/Prakash_Thete/9usq3enn/
I have optimized the above mentioned examples.
HTML
<div class="container">
<div class="row">
<div class="col-sm-6 form-group">
<div class="input-group" id="DateDemo">
<input type='text' id='weeklyDatePicker' placeholder="Select Week" />
</div>
</div>
</div>
</div>
JS
$(document).ready(function(){
moment.locale('en', {
week: { dow: 1 } // Monday is the first day of the week
});
//Initialize the datePicker(I have taken format as mm-dd-yyyy, you can //have your owh)
$("#weeklyDatePicker").datetimepicker({
format: 'MM-DD-YYYY'
});
//Get the value of Start and End of Week
$('#weeklyDatePicker').on('dp.change', function (e) {
var value = $("#weeklyDatePicker").val();
var firstDate = moment(value, "MM-DD-YYYY").day(0).format("MM-DD-YYYY");
var lastDate = moment(value, "MM-DD-YYYY").day(6).format("MM-DD-YYYY");
$("#weeklyDatePicker").val(firstDate + " - " + lastDate);
});
});
CSS
.bootstrap-datetimepicker-widget .datepicker-days table tbody tr:hover {
background-color: #eee;
}
Check the link for demo: https://jsfiddle.net/IamMHussain/ozdrdbqf/1/
I am currently working on a project with the same requirement. Don't know where I found it, but this fiddle on http://codepen.io/chanduvkm/pen/yeXKGW addresses almost the same issue in a slightly different way, but using the bootstrap-datepicker library you are currently using. The default value could be easily added. However, the CSS of the active week fails when the option 'weekStart: 1' is set, which is what I actually need (ISO-8601 week starts on Monday).
var startDate,
endDate;
$('#weekpicker').datepicker({
autoclose: true,
format :'mm/dd/yyyy',
forceParse :false
}).on("changeDate", function(e) {
//console.log(e.date);
var date = e.date;
startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay());
endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay()+6);
//$('#weekpicker').datepicker("setDate", startDate);
$('#weekpicker').datepicker('update', startDate);
$('#weekpicker').val((startDate.getMonth() + 1) + '/' + startDate.getDate() + '/' + startDate.getFullYear() + ' - ' + (endDate.getMonth() + 1) + '/' + endDate.getDate() + '/' + endDate.getFullYear());
});
In response to the previous answer by prakash, I forked the fiddle to start on Monday: https://jsfiddle.net/skfw0k53/ by adding the following:
moment.locale('en', {
week: { dow: 1 } // Monday is the first day of the week
});
So, basically two different libs to approach the problem.
You can try it.
Required file:
jquery-1.11.3.js
bootstrap-datepicker.min.js
bootstrap-datepicker.css
bootstrap.min.css
HTML
<div class="container">
<div class="row">
<div class='col-md-3'>
Week Start <select id="cweek">
<option value='0'> 0 </option>
<option value='1'> 1 </option>
<option value='2'> 2 </option>
<option value='3'> 3 </option>
<option value='4'> 4 </option>
<option value='5'> 5 </option>
<option value='6'> 6 </option>
<option value='7'> 7 </option>
</select>
</div>
<div class='col-md-3'>
Copy <select name='cweek_number' id='cweek_number'>
<option value='1'> 1 Week </option>
<option value='2'> 2 Week </option>
<option value='3'> 3 Week </option>
<option value='4'> 4 Week </option>
</select> from
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="from_cal"></div>
</div>
<div class="row">
<div class="to_cal"></div>
</div>
</div>
</div>
js
var weekOfStart = 0;
var startDate;
var endDate;
var toCal_startDate;
var toCal_endDate;
var setNumberOfWeek = 1;
var from_cal = '.from_cal';
var to_cal = '.to_cal';
var obj = $(from_cal).datepicker({
weekStart:weekOfStart,
maxViewMode: 0,
onSelect: function (date) {
},
beforeShowDay: function(date)
{
var cssClass = '';
if(date >= startDate && date <= endDate)
cssClass = 'oui-state-hover';
return [true, cssClass];
}
// startDate: new Date()
});
$(from_cal).on('changeDate', function(evt) {
var date = evt.date;
var dayAdjustment = date.getDay() - weekOfStart;
if (dayAdjustment < 0) {
dayAdjustment += 7;
}
if(setNumberOfWeek > 1)
{
weekLast = 7*(setNumberOfWeek-1);
}
else {
weekLast = 0;
}
startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - dayAdjustment);
endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - dayAdjustment + (6+weekLast));
// console.log(startDate);
// console.log(endDate);
$(this).find('.active').closest('tr').addClass('oui-selected');
if(setNumberOfWeek > 1)
{
$(this).find('.active').closest('tr').nextAll().slice(0, (setNumberOfWeek-1)).addClass('oui-selected');
}
});
$(from_cal).on('mousemove', '.table-condensed tr', function () {
$(this).find('td').addClass('oui-state-hover');
if(setNumberOfWeek > 1)
{
$(this).nextAll().slice(0, (setNumberOfWeek-1)).find('td').addClass('oui-state-hover');
}
});
$(from_cal).on('mouseleave', '.table-condensed tr', function () {
$(this).find('td').removeClass('oui-state-hover');
$(this).nextAll().find('td').removeClass('oui-state-hover');
});
$(from_cal).find('td').on('click', function(){
console.log('ok');
});
obj.datepicker();
// To date Calendar code
var toCalObj = $(to_cal).datepicker({
weekStart:weekOfStart,
maxViewMode: 0,
// startDate: new Date()
});
toCalObj.datepicker();
$(to_cal).on('mousemove', '.table-condensed tr', function () {
$(this).find('td').addClass('kui-state-hover');
if(setNumberOfWeek > 1)
{
$(this).nextAll().slice(0, (setNumberOfWeek-1)).find('td').addClass('kui-state-hover');
}
});
$(to_cal).on('mouseleave', '.table-condensed tr', function () {
$(this).find('td').removeClass('kui-state-hover');
$(this).nextAll().find('td').removeClass('kui-state-hover');
});
$(to_cal).on('changeDate', function(evt) {
var date = evt.date;
var dayAdjustment = date.getDay() - weekOfStart;
if (dayAdjustment < 0) {
dayAdjustment += 7;
}
if(setNumberOfWeek > 1)
{
weekLast = 7*(setNumberOfWeek-1);
}
else {
weekLast = 0;
}
toCal_startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - dayAdjustment);
toCal_endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - dayAdjustment + (6+weekLast));
console.log(toCal_startDate);
console.log(toCal_endDate);
$(this).find('.active').closest('tr').addClass('oui-selected');
if(setNumberOfWeek > 1)
{
$(this).find('.active').closest('tr').nextAll().slice(0, (setNumberOfWeek-1)).addClass('oui-selected');
}
// console.log(date.getDay());
});
//comman code
$('#cweek').change(function()
{
weekOfStart = $('#cweek').val();
obj.datepicker('destroy');
toCalObj.datepicker('destroy');
obj.datepicker({weekStart:$('#cweek').val(),maxViewMode:0}).datepicker('update');
toCalObj.datepicker({weekStart:$('#cweek').val(),maxViewMode:0}).datepicker('update');
});
$('#cweek_number').change(function()
{
setNumberOfWeek = $('#cweek_number').val();
obj.datepicker('destroy');
toCalObj.datepicker('destroy');
obj.datepicker({weekStart:$('#cweek').val(),maxViewMode:0}).datepicker('update');
toCalObj.datepicker({weekStart:$('#cweek').val(),maxViewMode:0}).datepicker('update');
});
$('.table-condensed').addClass('table');
Screen Shot
JSFiddle link https://jsfiddle.net/shivamanhar/ddphp8jx/9/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With