Future week disabled in Jquery UI Calender - jquery-ui

I need to disabled future weeks for jqueryUI calender, i tried something which is disabled future date. but i want future week disable and it's disabled the other dates of week 52 as well. please if anyone can help me in this. below is link of my work. in this you can see disabled dates of week 52. i want whole week selectable.
Fiddle Link of so far i done.
<input type="text" class="form-control weekcal-x" name="start" id="start" custom="" />
$(".weekcal-x").datepicker({
showWeek: true,
firstDay: 1,
maxDate: new Date,
onSelect: function(dateText, inst) {
$(this).val("Week " + $.datepicker.iso8601Week(new Date(dateText)) + ', ' + $.datepicker.formatDate("yy", new Date(dateText)));
}
}).datepicker('widget').addClass('ui-weekpicker');
$('.ui-weekpicker').on('mousemove', 'tr', function() {
$(this).find('td a').addClass('ui-state-hover');
});
$('.ui-weekpicker').on('mouseleave', 'tr', function() {
$(this).find('td a').removeClass('ui-state-hover');
});

Try this
maxDate: +7 instead of maxDate: new Date
Update
Get the first and last day of current week :
var currentDate = new Date; // get current date
var first = currentDate.getDate() - currentDate.getDay() + 1; // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var firstday = new Date(currentDate.setDate(first));
var lastday = new Date(currentDate.setDate(last));
Now you can use them as you want like maxDate: lastday, minDate:firstday

Related

jquery ui - disable today and highlight next 3 days

So far I can disable Today's date, but I'm coming up short trying to highlight the next 3 days
$( "#someDiv" ).datepicker({
beforeShowDay: function( date ){
//disable Sundays;
return [date.getDay() != 0, '']
},
/* today is disabled */
minDate: 1
});
... or is there a way to render individual day cells with date info as data attributes or something like that?
In your return, add a condition that will check for the date range you want and add a class to those dates.
Here is a jsFiddle with the full example. I'm sure this can be improved upon though.
The code and CSS to add a background to the dates when the condition is true (style it how you like):
.highlightDay .ui-state-default {
background: #484;
color: #FFF;
}
$(document).ready(function() {
$("#datepicker").datepicker({
beforeShowDay: function(date) {
var newDate = addDays(new Date(), 0);
var thirdDay = addDays(new Date(), 3);
return [date.getDay() != 6,
// This can probably be improved
date >= newDate && date <= thirdDay ? "highlightDay" : ""];
},
minDate: 1
});
});
function addDays(theDate, days) {
return new Date(theDate.getTime() + days*24*60*60*1000);
}

Jquery UI Datepicker with EOM and MOM

I need implement a Jquery UI Calendar that when a users click any date, it select automatically to the next MOM or EOM.
For example, if the user clicks on April 9th, it must go to 'April 15th',
or if clicks on 22 February, it must go to 'Feb 28' or 'Feb 29'.
For clarification:
MOM is "Middle of Month, every time is 15th", from 1 to 15 of any month.
EOM is "End of Month, it's the last day of the month, jan 31, feb 28 or 29, mar 31, apr 30, may 31, jun 30, july 31, aug 31, sep 30, oct 31, nov 30, dec 31...
Thanks
I would do this as a standard jquery onchange handler for the text box. The reason is that a user can bypass the date-picker and type in a date manually. You want the date to automatically be changed no matter if the date is selected, typed in, or pasted in.
Use the onSelect event from datepicker. Be aware that if you have maxDate option set and its before EOM or MOM the value selected will be maxDate.
UPDATE: Close after select. Use onClose instead.
UPDATE2: jsFiddle using latest jQuery UI
onClose: function(dateText, inst) {
var day = inst.currentDay,
month = inst.currentMonth,
year = inst.currentYear;
if (day <= 15) {
day = 15;
}
else {
day = 0;
if (month>10){
month = 0;
year++;
}
else
month++;
}
var d = new Date(year, month, day);
$(this).datepicker("setDate", d);
}
The code that works with IE6/IE7/IE8/IE9 and FF, Chrome, etc.
<script type="text/javascript">
$(function() {
$("#payperiodcalendar input").datepicker({
dateFormat: 'dd-M-yy',
onSelect: function(dateText, inst) {
var selectedDay = inst.currentDay,
selectedMonth = inst.currentMonth,
selectedYear = inst.currentYear;
if (selectedDay <= 15) {
var now = new Date(selectedYear, selectedMonth, 15);
} else {
var now = new Date(selectedYear, selectedMonth, LastDayOfMonth(selectedYear, selectedMonth));
}
$(this).attr("value", now.format("dd-MMM-yyyy"));
}
});
function LastDayOfMonth(Year, Month) {
Month++;
return (new Date((new Date(Year, Month, 1)) - 1)).getDate();
}
});
</script>

jQuery UI datepicker onChangeMonthYear and the inst parameter

$('.selector').datepicker({
onChangeMonthYear: function(year, month, inst) { ... }
});
How can I use the 'inst' parameter of onChangeMonthYear to auto-select the first day of the month?
see: http://jsfiddle.net/sD8rL/
I am currently using the code below but I feel like I should be able to use the 'inst' variable in a more straight-forward way.
$(".datepicker" ).datepicker({
changeMonth: true,
changeYear: true,
maxDate:0,
onChangeMonthYear: function(year, month, inst){
// set date to 1st on year or month change
// this seems bit janky, but works
$('#' + inst.id).datepicker( "setDate", month + '/1/' + year );
// Can't I use the instatnce to set the date?
// $(inst).datepicker( "setDate", month + '/1/' + year ); // fails
// inst.datepicker( "setDate", month + '/1/' + year ); // fails
// inst.selectedDay = 1; // fails
// inst.currentDay = 1; // fails
}
});
If there is no particular reason that you want to use inst, you can always use this:
onChangeMonthYear: function(year, month, inst){
$(this).datepicker( "setDate", month + '/1/' + year );
}
See it in action: http://jsfiddle.net/william/sD8rL/2/.
Solution by William Niu doesn't consider other date formats (such as dd-mm-yyyy).
You'd better get a Date object from your datepicker and modify it the way you like (i.e., setting the first day of the month). After your modifications, you can re-pass the modified Date object to your datepicker.
dateFormat: "dd-mm-yy",
onChangeMonthYear: function(year, month, inst){
var selectedDate = $(this).datepicker( "getDate" );//Date object
selectedDate.setDate(1);//set first day of the month
selectedDate.setMonth(month-1);//month is 1-12, setMonth is 0-11
selectedDate.setFullYear(year);
$(this).datepicker( "setDate", selectedDate );
}
This way you won't overwrite date format (possibly set during datepicker initialization) ;)
Pay attention to the month format: datepicker handles a 1-12 format for months, while the Date object is 0-11.
Hope this can help, bye!

Show months outside date range in selector?

I'm using jQuery UI's DatePicker to select a date of birth for consumers on my website. It is currently showing with the month and year via drop downs When, for example, I restrict the years to -18y, via maxDate, the month drop down does not show all of the months when the year 1993 is selected. It only shows the months up until the maximum months.
In usability testing, we've found that our demographic tends to click the month of their birth, then the year, then the day.
Is there a way to show all of the months, even if the dates within that month are not selectable?
Here is the code used to show the DatePicker:
$('.DobWidget').datepicker({
showOn: 'both',
buttonImage: '/media/img/admin/icon_calendar.gif',
buttonImageOnly: true,
dateFormat: js_date_format, // mm/dd/yyyy
constrainInput: false, // Handled server-side for type-in.
changeMonth: true,
changeYear: true,
maxDate: '-18y',
minDate: '-110y',
showButtonPanel: true,
onChangeMonthYear: function(year, month, inst) {
if (inst.currentDay != 0) {
$(this).datepicker("setDate", new Date(year, month - 1, inst.currentDay));
}
}
});
I am currently using jQueryUI v.1.8.16, and jQuery v.1.6.3, both provided by the Google AJAX API's.
Fix for jquery ui 1.8.17
I also had this problem I got in to work by changing some things in the jquery code. Now I see all months and I can select them but dates will still be unselectable as you want.
In _generateHTML function in the if statement you add maxDraw.setMonth('11'):
if (maxDate) {
maxDraw = (minDate && maxDraw < minDate ? minDate : maxDraw);
maxDraw.setMonth('11');
while (this._daylightSavingAdjust(new Date(drawYear, drawMonth, 1)) > maxDraw) {
...
}
}
In _generateMonthYearHeader function in the for loop of the else statement you change:
for (var month = 0; month < 12; month++) {
if ((!inMinYear || month >= minDate.getMonth()) &&
(!inMaxYear || month <= maxDate.getMonth()))
monthHtml += '<option value="' + month + '"' +
(month == drawMonth ? ' selected="selected"' : '') +
'>' + monthNamesShort[month] + '</option>';
}
becomes
for (var month = 0; month < 12; month++) {
if ((!inMinYear || month >= minDate.getMonth()) &&
(!inMaxYear || month <= 11))
monthHtml += '<option value="' + month + '"' +
(month == drawMonth ? ' selected="selected"' : '') +
'>' + monthNamesShort[month] + '</option>';
}
In _restrictMinMax you add maxDate.setMonth('11'):
...
var maxDate = this._getMinMaxDate(inst, 'max');
maxDate.setMonth('11');
var newDate = (minDate && date < minDate ? minDate : date);
...
In _isInRange you add maxDate.setMonth('11'):
var minDate = this._getMinMaxDate(inst, 'min');
var maxDate = this._getMinMaxDate(inst, 'max');
maxDate.setMonth('11');
...

jQuery UI - Datepicker - Hide year

I'm using jQuery UI 1.8 and I would like to hide the year from the user in both the popup and the textbox. Essentially instead of picking the day, month and year I want the user to just pick the day and month.
Hiding the year in the textbox is easy enough, the code shown below will do that. I'm stumped on how to hide the year from the popup - so it would say "April" instead of "April 2010".
$(function() {
$("#beginDateRange").datepicker({ dateFormat: 'mm/dd' });
});
<input type="text" name="beginDateRange" id="beginDateRange" />
Any help would be greatly appreciated.
I came across this thread in my search for a good way to do this, and here's what i came up with.
in my css i have a
.BirthdayDatePicker .ui-datepicker-year
{
display:none;
}
and this is how i set up my datepickers that i don't want to show the year on:
$('.DateTextBox.NoYear').datepicker({
beforeShow: function (input, inst) {
inst.dpDiv.addClass('BirthdayDatePicker');
},
onClose: function(dateText, inst){
inst.dpDiv.removeClass('BirthdayDatePicker');
}
});
basically i add the class just before it shows, and take it off when it hides it, so that other date pickers on the page arn't affected by it.
just incase anyone ever needs to hide the year only on specific pickers on a page and doesn't want to mess with the internals of jQuery.
I dont think this option is exposed va the api.
I belive that the easiest way is to change the stylesheet.
Change the ui-datepicker-year class to display: none
Another option would be to edit the source so it isnt rendered at all,
to do that you can remove this part of the code:
// year selection
if (secondary || !changeYear)
html += '<span class="ui-datepicker-year">' + drawYear + '</span>';
else {
// determine range of years to display
var years = this._get(inst, 'yearRange').split(':');
var thisYear = new Date().getFullYear();
var determineYear = function(value) {
var year = (value.match(/c[+-].*/) ? drawYear + parseInt(value.substring(1), 10) :
(value.match(/[+-].*/) ? thisYear + parseInt(value, 10) :
parseInt(value, 10)));
return (isNaN(year) ? thisYear : year);
};
var year = determineYear(years[0]);
var endYear = Math.max(year, determineYear(years[1] || ''));
year = (minDate ? Math.max(year, minDate.getFullYear()) : year);
endYear = (maxDate ? Math.min(endYear, maxDate.getFullYear()) : endYear);
html += '<select class="ui-datepicker-year" ' +
'onchange="DP_jQuery_' + dpuuid + '.datepicker._selectMonthYear(\'#' + inst.id + '\', this, \'Y\');" ' +
'onclick="DP_jQuery_' + dpuuid + '.datepicker._clickMonthYear(\'#' + inst.id + '\');"' +
'>';
for (; year <= endYear; year++) {
html += '<option value="' + year + '"' +
(year == drawYear ? ' selected="selected"' : '') +
'>' + year + '</option>';
}
html += '</select>';
}
I haven't tried removing the code but it should work.
I did try hiding it using css and that does work (in firefox anyway :) )
HTH
Very old question, but I just needed to do this myself and here's an alternate method that might be useful to somebody; a quick and dirty fix that will enable your other datepickers to continue working, provided you do NOT need the changeYear functionality is to set changeYear to true on the datepickers you DON'T want a year showing up, then add the CSS:
select.ui-datepicker-year { display:none }
VIKSME Hide year http://jsfiddle.net/tocv/e5cvA/
CSS
.ui-datepicker-year
{
display:none;
}
HTML
<div id="datepicker"></div>
</div>
JavaScript
$(function() {
$( "#datepicker" ).datepicker({
changeMonth: true,
changeYear: false
});
});
The attribute
changeYear:false;
select.ui-datepicker-year { display:none }
sometimes change dropdown to span.<span></span>
So add this css code.
span.ui-datepicker-year { display:none }

Resources