I wonder how to set next month with showing only mondays active:
i tried to do smth like that but it wont work
function onlyMondaysNextMonth(date){
var day = date.getDay();
var mDate = date.getMonth() + 1;
return {
minDate: mDate,
}
return [(day == 1),''];
}
Thank you.
Use the following code to enable only Mondays starting from next month
var minDate = null;
var now = new Date();
if (now.getMonth() == 11) {
minDate = new Date(now.getFullYear() + 1, 0, 1);
} else {
minDate = new Date(now.getFullYear(), now.getMonth() + 1, 1);
}
/* create datepicker */
jQuery(document).ready(function () {
jQuery('#datepicker').datepicker({
minDate: minDate,
constrainInput: true,
beforeShowDay: beforeShowDay
});
});
function beforeShowDay(date) {
var day = date.getDay();
if (day == 1)
return [true]
return [false];
}
The working sample is hosted in http://elangovanr.com/samples/jquery/datepickermonday.html for your reference.
Related
Good afternoon. In my project, I need to display a calendar for a year. I did it with datepicker. Now I need to make it so that I can select the year of this calendar from another page. Please tell me how to do this? (A year comes to the page with a request)
function drawDatepicker(){
$('#picker').datepicker({
showOtherMonths: true,
selectOtherMonths: true,
numberOfMonths: [3,4],
stepMonths: 0,
yearRange: chouseYear + ":" + chouseYear,
showCurrentAtPos: new Date().getMonth(),
onSelect: function (dateText, datePicker) {
datePicker.drawMonth += $("#picker").datepicker("option", "showCurrentAtPos");
var radVal = $('input[name="typeDay"]:checked').val();
if(radVal == 1){
addOrRemoveDate(dateText, day_off);
}
if(radVal == 2){
addOrRemoveDate(dateText, radonica);
}
console.log(day_off);
console.log(radonica);
},
beforeShowDay: function (date) {
var year = date.getFullYear();
var month = padNumber(date.getMonth() + 1);
var day = padNumber(date.getDate());
var dateString = day + "." + month + "." + year;
var gotDate1 = jQuery.inArray(dateString, day_off);
if (gotDate1 >= 0) {
return [true, "ui-state-highlight"];
}
var gotDate2 = jQuery.inArray(dateString, radonica);
if (gotDate2>=0){
return [true, "ui-widget-header"];
}
return [true, ""];
}
});
}
Screen calendar
I found this solution to the problem. I specified defaultDate 01.01 of the desired year and showCurrentAtPos to 0. If the current year is specified, then defaultDate is today, and showCurrentAtPos is the number of the month
defaultDate: defaultDate,
showCurrentAtPos: sCAPos,
Sorry for posting this simple question. Application is going to be deployed soon, and recently a bug came, but i am not able to figure out why it is comming.
Created A JS_BIN this is the link
http://jsbin.com/razufavuru/1/edit?html,js,output
Error is minDate: 0 is disabling today's date also while i want to disable only past dates.
This is jquery code:
$("#leaveEndDate").datepicker(
{
minDate: 0,
beforeShowDay : disablePublicHolidaysAndWeekends,
minDate : dateToday,
onSelect : function(dateText, inst) {
var firstDate = $("#leaveStartDate").datepicker('getDate');
var selDate = null;
selDate = $(this).datepicker('getDate');
var lastDate = selDate;
if (lastDate < firstDate) {
$("#leaveEndDate").val('');
$("#endDateError").html(
"End Date must be greater than Start Date");
$(inst).datepicker('show');
} else {
$("#endDateError").html("");
calculateDays(firstDate, lastDate);
}
}
});
Disable Holiday and Weekends Method is
function disablePublicHolidaysAndWeekends(date) {
var month = date.getMonth();
var day = date.getDate();
var year = date.getFullYear();
if(day < 10 && day > 0){
day = '0'+day;
}
var getdate = year+ '/' + '0' +(month + 1) + '/' + day;
for (var i = 0; i < publicHolidayDates.length; i++) {
if ($.inArray( getdate ,publicHolidayDates) != -1 || new Date() > date) {
return [ false ];
}
}
var noWeekend = $.datepicker.noWeekends(date);
return !noWeekend[0] ? noWeekend : [ true ];
}
this is the img
public holidays are comming from database.
It confuse me lot when this code for another datapicker works.
$("#targetDate").datepicker({
beforeShowDay : $.datepicker.noWeekends,
minDate : 0
});
Here it do not disable today date.
this is the img
jQuery Datepicker value minDate expect value of type
Type: Date or Number or String
Try minDate: new Date() instead, or minDate: '0'
UPDATE:
What causes your issue is your custom function:
function disablePublicHolidaysAndWeekends(date) {
var month = date.getMonth();
var day = date.getDate();
var year = date.getFullYear();
if(day < 10 && day > 0){
day = '0'+day;
}
if(month < 9 && month > 0){ //added control over month, as you would have got errors for month=10, 11, 12 in the below variable getdate
month = '0' +(month + 1);
}
else{
month += 1;
}
var getdate = year+ '/' + month + '/' + day;
for (var i = 0; i < publicHolidayDates.length; i++) {
//removed "|| new Date() > date"
if ($.inArray( getdate ,publicHolidayDates) !== -1 ) {
return [ false ];
}
}
var noWeekend = $.datepicker.noWeekends(date);
return !noWeekend[0] ? noWeekend : [ true ];
}
Read this about Date comparison in javascript
Fiddle here
I want to show only month and year. If I refocused same input after select a date, it shows wrong month and year not show previously selected month and year. For example I select December 2010 and then refocused same input it shows April 2015 as below image. How can I?
HTML
<input type="text" class="dp" readonly="true">
CSS
.ui-datepicker-calendar {
display: none;
}
JS
$('.dp').datepicker({
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'mm.yy',
onClose: function (dateText, inst) {
$(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay));
}
});
http://jsfiddle.net/46tx62vu/
The datepicker expects to be able to parse the displayed version of the date into a javascript date object.
One way to do this is to add a custom parseDate function for your special date format. The code below looks for the special format of "mm.yy" and if found, parses the value into a date. If the format is anything else, pass processing to the original datepicker.parseDate.
var standardParseDate = $.datepicker.parseDate;
function customParseDate( format, value, options ) {
if (format === 'mm.yy') {
var dt = (value + ".").split(".");
var month = parseInt(dt[0]) - 1;
var year = parseInt(dt[1]);
if (year < 100) {
year += 2000;
}
if (isNaN(month) || isNaN(year)) {
return null;
}
return new Date(year, month, 1);
} else {
return standardParseDate(format,value,options);
}
}
$.datepicker.parseDate = customParseDate;
JSFiddle: http://jsfiddle.net/3drfd35t/
additional code for customParseDate:
} else if (format === 'M-yy') {
var datestr = value;
if (datestr.length > 0) {
var y = datestr.substring(datestr.length - 4, datestr.length); // last 4 chars
var m = jQuery.inArray(datestr.substring(0, datestr.length - 5), options.monthNamesShort);
var newDate = new Date(y, m, 1);
console.log("customParseDate " + "m:" + m + ", y:" + y); // dbg
return newDate;
} else {
console.log("customParseDate " + "null"); // dbg
return null;
}
} else . . .
I'm in creating appointment form with jQuery datepicker. I've search around but seems that I can't combine all function I want in the beforeshowday.
What I want from the datepicker is disabled all date before today (yesterday and the rest of it because you can't make appointment at date before today it must be later), then disabled on every Sunday (its non working day) and public holiday (this one using array). What I saw from others is the jQuery are specifically for only one function like public holiday it just an array, but how about disabled previous day and sunday?
I tried to follow this articles http://articles.tutorboy.com/2010/09/03/jquery-ui-datepicker-disable-specified-dates/ but I don't know how to combine it. Can someone show me how?
I have this to disabled on every Sunday
function disabledSunday(date) {
var day = date.getDay();
return [(day != 0), ''];
}
$('#datepicker').datepicker({
dateFormat: 'mm-dd-yy',
beforeShowDay: disabledSunday
});
This one for alldates till today
var date = new Date();
var m = date.getMonth(),
d = date.getDate(),
y = date.getFullYear();
// Disable all dates till today
$('#datepicker').datepicker({
minDate: new Date(y, m, d),
dateFormat: 'mm-dd-yy',
});
This one is for specific dates such as public holiday
// Disable a list of dates
var disabledDays = ["5-31-2013", "6-01-2013"];
function disableAllTheseDays(date) {
var m = date.getMonth(),
d = date.getDate(),
y = date.getFullYear();
for (i = 0; i < disabledDays.length; i++) {
if ($.inArray((m + 1) + '-' + d + '-' + y, disabledDays) != -1) {
return [false];
}
}
return [true];
}
$('#datepicker').datepicker({
dateFormat: 'mm-dd-yy',
beforeShowDay: disableAllTheseDays
});
How to combine these three function into one, I'm not much into Jquery and javascript
try this :-
html code :
<input id="txtDate" />
function disabledays(date) {
var ymd = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
//if u have to disable a list of day
var removeDays = ["2013-6-11","2013-6-31" ];
if ($.inArray(ymd, removeDays) >= 0) {
return [false];
} else {
//Show accept sundays
var day = date.getDay();
return [(day == 1 || day == 2 || day == 3 || day == 4 ||day == 5 ||day == 6 )];
}
}
$(function () {
$('#txtDate').datepicker({
beforeShowDay: disabledays
});
});
Try this
$("#datepicker").datepicker({ minDate: 0 });
You can use minDate option to disable past dates. In addition, you can use beforeShowDay option to check the other two conditions**.
$("#datepicker").datepicker({
minDate: 0,
beforeShowDay: function (date) {
// it is possible to write the following function using one line
// of code; instead, multiple if/else are used for readability
var ok = true;
if (date.getDay() === 0) { // is sunday
ok = false;
} else {
var dateStr = $.datepicker.formatDate("m-dd-yy", date);
if ($.inArray(dateStr, disabledDays) >= 0) { // is holiday
ok = false;
}
}
return [ok, ""];
}
});
});
** Actually it is possible to check all three conditions in that function.
May u will find your slution here
http://jqueryui.com/datepicker/#min-max
I have some problem with hiding days in datepicker depending of day of week e.g. if today is friday hide saturday and if today is saturday - hide sunday.
I have this code who check what day is today:
$(function () {
var day_date = new Date();
var weekday = new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
var n = weekday[day_date.getDay()];
$('#day_of_week').val(n);
});
I also have this code to hide (but whole) weekends and days who always be hidden:
var disabledDays = ['15/8/2012', '1/11/2012', '11/11/2012', '25/12/2012', '26/12/2012'];
function nationalDays(date) {
var m = date.getMonth(),
d = date.getDate(),
y = date.getFullYear();
for (i = 0; i < disabledDays.length; i++) {
if ($.inArray(d + '/' + (m + 1) + '/' + y, disabledDays) != -1 || new Date() > date) {
return [false];
}
}
return [true];
}
function noWeekendsOrHolidays(date) {
var noWeekend = jQuery.datepicker.noWeekends(date);
return noWeekend[0] ? nationalDays(date) : noWeekend;
}
On datepicker the line who "execute" above code looks like this:
beforeShowDay: noWeekendsOrHolidays,
I try to do this with this code, but it didn't work:
$('#day_of_week').change(function()
if( $("#day_of_week").val() == Friday ) {
$("#date_from, #date_to").datepicker({
beforeShowDay: noWeekendsOrHolidays
});
}
else {
}
});
I will be very grateful for any help.
I am using a date picker in which it automatically work on hiding date.
I hope it will helpful to you.
http://multidatespickr.sourceforge.net/#maxPicks-demo
I got a link regarding this and hopefull it will work.
http://multidatespickr.sourceforge.net