我想知道如何教Jquery UI datepicker只显示当前周(周一至周日)和两个按钮上/下周。
由于
答案 0 :(得分:0)
没有办法让jQuery UI datepicker显示几周。 我还没有看到任何插件,现在我想到了它。
您可以从github获取源代码并根据您的需要进行修改,但这需要花费大量时间。 https://github.com/jquery/jquery-ui
如果您需要逐周显示,因为您只想选择一周而不是一天。然后你可以使用wijmo日历。这允许选择(不显示)周。 http://wijmo.com/Wijmo-Open/samples/#calendar|selection(启用天数和周数)
答案 1 :(得分:0)
您只是查看了一些方便的片段,为自己构建了最小版本
init: function() {
// Get today (or any other date)
var today = new Date();
// Get monday
var monday = this.getMonday(today);
// Render the week
this.renderWeekFromMonday(monday);
},
renderWeekFromMonday : function(date) {
var dayContainer = $('#dayContainer');
// clear div
dayContainer.empty();
// Render days
for (var i = 0; i <= 7; i++) {
// Get mondday day (1-31)
var day = date.getDate();
// Today
var t = new Date();
// Create dayobject for usage inside for loop
var d = new Date(date);
// Render 7 days (1 week)
for (var i = 0; i < 7; i++) {
// New day (+1)
d.setDate(day + i)
// Create html
var span = $("<span>").addClass("day").attr("time", d.getTime())
span.html("<span class=dayNumber>" + d.getDate() + "</span>");
// Append day
dayContainer.append(span);
}
}
},
getMonday: function (date) {
// Get the day of the week for the specified date. 0 = Sun, 1 = Mon etc.
var day = date.getDay(),
diff = date.getDate() - day + (day == 0 ? -6:1); sunday ?
return new Date(date.setDate(diff));
}
答案 2 :(得分:0)
从日期选择器中选择后,您希望显示多少天,您可以拒绝该功能中的星期五可变。 (我将周末定为假期) $(函数() {
$('#ToDate').val = null;
$.datepicker.setDefaults($.datepicker.regional['en']);
$('#FromDate').datepicker(
{
firstDay: 0,
beforeShowDay: $.datepicker.noWeekends,
onSelect: function (selectedDate) {
var date = $('#FromDate').datepicker('getDate');
var adddate = date;
adddate.setDate(date.getDate() + 5); // Add 7 days
//$('#FromDate').datepicker('option', 'minDate', 0);
//$('#ToDate').datepicker('setDate', $('#FromDate').datepicker('getDate'));
//$('#ToDate').datepicker('option', 'maxDate', adddate);
$('#ToDate').datepicker('option', 'minDate', $('#FromDate').datepicker('getDate'));
$('#ToDate').datepicker('option', 'maxDate', adddate);
//var weekday = $(this).datepicker('getDate');
//$('#Lbl1').val = weekday;
}
});
//var Fromdate = $('#FromDate').datepicker('getDate');
//var fromyear = Fromdate.getFullYear(); //get year
//var frommonth = Fromdate.getMonth();
//var fromdate = Fromdate.getDate();
//var fromtime = Fromdate.getTime();
//$('#ToDate').datepicker(
// {
// //stepMonths: 0,
// onSelect: function (selectedDate) {
// var Fromdate = $('#FromDate').datepicker('getDate');
// var Todate = $('#ToDate').datepicker('getDate');
// //var a = calcDate(Fromdate, Todate);
// //alert(a);
// //myfunc();
// days_between(Fromdate, Todate);
// }
// });
//function days_between(Fromdate, Todate) {
// var date = $('#FromDate').datepicker('getDate');
// var adddate = date;
// adddate.setDate(date.getDate() + 7);
// // The number of milliseconds in one day
// var ONE_DAY = 1000 * 60 * 60 * 24;
// // Convert both dates to milliseconds
// var date1_ms = Fromdate.getTime();
// var date2_ms = Todate.getTime();
// // Calculate the difference in milliseconds
// var difference_ms = Math.abs(date1_ms - date2_ms);
// // Convert back to days and return
// if (parseInt(Math.round(difference_ms / ONE_DAY)) >= adddate) {
// alert("u cant select more than seven days");
// }
// else {
// $('#FromDate').datepicker('getDate') === null;
// }
//}
//$("#ToDate").datepicker(
// {
// //beforeShowDay: $.datepicker.noWeekends ,
// //In Datepicker set the Calendar display start with Monday
// //firstDay: $('#FromDate').datepicker('getDate'),
// ////stepMonths: 0,
// ////month: date.getMonth(),
// //showOtherMonths: true,
// //selectOtherMonths: true,
// ////startDate: new Date(month, date1, year), //set it here
// ////endDate: new Date(month, '31', year + 1),
// // beforeShowDay: function (date)
// // {
// // //var monday = new Date("June 1, 2013 00:00:00"); Used it for testing
// // //Get today's date
// // var monday = $('#FromDate').datepicker('getDate');
// // //Set the time of today's date to 00:00:00
// // monday.setHours(0, 0, 0, 0);
// // monday.setDate(monday.getDate() + 1 - (monday.getDay() || 7));
// // //Set the Date to Monday
// // var sunday = new Date(monday);
// // //Now add 6 to Monday to get the Date of Sunday (End of that Week)
// // sunday.setDate(monday.getDate() + 6);
// // //Now return the enabled and disabled dates to datepicker
// // return [(date >= monday && date <= sunday), ''];
// // },
// //beforeShowDay: $.datepicker.noWeekends ,
// onSelect: function (selectedDate)
// {
// var Fromdate = $('#FromDate').datepicker('getDate');
// var Todate = $('#ToDate').datepicker('getDate');
// //var a = calcDate(Fromdate, Todate);
// //alert(a);
// //myfunc();
// days_between(Fromdate, Todate);
// }
//});
//Set the date format to dd/mm/yy ie., 30/10/1989
$(function () {
$('#ToDate').datepicker(
{
firstDay: $('#FromDate').datepicker('getDate'),
//stepMonths: 0,
//month: date.getMonth(),
showOtherMonths: true,
selectOtherMonths: true,
//startDate: new Date(month, date1, year), //set it here
//endDate: new Date(month, '31', year + 1),
beforeShowDay: function (date) {
//var monday = new Date("June 1, 2013 00:00:00"); Used it for testing
//Get today's date
var monday = $('#FromDate').datepicker('getDate');
//Set the time of today's date to 00:00:00
monday.setHours(0, 0, 0, 0);
monday.setDate(monday.getDate() + 1 - (monday.getDay() || 7));
//Set the Date to Monday
var friday = new Date(monday);
//Now add 6 to Monday to get the Date of Sunday (End of that Week)
friday.setDate(monday.getDate() + 4);
//Now return the enabled and disabled dates to datepicker
return [(date >= monday && date <= friday), ''];
},
onClose: function (selected) {
if (selected.length <= 0) {
// selected is empty
$("#ToDate").datepicker('disable');
} else {
$("#ToDate").datepicker('enable');
}
$("#ToDate").datepicker("option", "dateFormat", "mm/dd/yy");
}
});
});
$("#ToDate").datepicker("option", "dateFormat", "mm/dd/yy");
//Ending the block
});