jquery ui datepicker add 6 months is wrong in edge cases - javascript

I'm using datepicker to set the end date to be 6 months after the start date. What I have is working except in edge cases. Example. If start date is 31-Mar-2014 then the end date should be 30-Sep-2014. However I get 01-Oct-2014. I've read about this being a known issue in other thread comments Here
and Here.
Is there something in JQuery or datepicker or an easy way that I can correct the date in edge cases like this?
There is likely an answer to this already but I haven't been able to scout it out. Any help would be greatly appreciated.
Here is the stripped down code and a demo I made that shows the example.
$("#StartDate").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: "dd-M-yy",
onSelect: function () {
autoFillEndDate();
}
});
$("#EndDate").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: "dd-M-yy"
});
function autoFillEndDate() {
if ($('#StartDate').val().length !== 0) {
var offset;
var offsetType;
var StartDate = $('#StartDate').datepicker('getDate');
var newDate = StartDate.setMonth(StartDate.getMonth() + 6);
var EndDT = $.datepicker.formatDate('dd-M-yy', new Date(newDate));
$('#EndDate').datepicker("setDate",EndDT);
}
}

Have you seen this answer ?
Updated your fiddle with that given function and it gets 30-sep:
http://jsfiddle.net/z3yshuzc/1/
function addMonths(dateObj, num) {
var currentMonth = dateObj.getMonth() + dateObj.getFullYear() * 12;
dateObj.setMonth(dateObj.getMonth() + num);
var diff = dateObj.getMonth() + dateObj.getFullYear() * 12 - currentMonth;
// If don't get the right number, set date to
// last day of previous month
if (diff != num) {
dateObj.setDate(0);
}
return dateObj;
}

Related

Choose year for Datepicker

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,

JQuery UI Datepicker - MaxDate exclude disabled days

I am trying to exclude disabled dates when counting MaxDate.
I tried many ways but still doesn't work.
Any suggestion?
02-12-2019 and Sundays has been disabled but the Maxdate include the disabled date.
Maxdate should be 3 days which excludes disabled days and Maxdays starts by today.
My goal is to add days if the days between today until max days has disabled.
Add 1 day per disabled day
Update
Now i am able to Exclude sunday when counting maxdate but i still can't exclude the array date where it should add one more day after 02-12-2019.
Updated Code :(
<script>
var array = ["02-12-2019"]
//new
function includeDate(date) {
return date.getDay() !== 7 && date.getDay() !== 0;
}
function getTomorrow(date) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1);
}
//prev
$('input').datepicker(
{
defaultDate: "+1d",
inline: true,
showOtherMonths: true,
changeMonth: true,
selectOtherMonths: true,
required: true,
showOn: "focus",
numberOfMonths: 1,
minDate: 1,
beforeShowDay: function(date) {
var string = jQuery.datepicker.formatDate('dd-mm-yy', date);
var isDisabled = ($.inArray(string, array) != -1);
return [includeDate(date) && !isDisabled];
},
maxDate: (function(max) {
var today = new Date();
var nextAvailable = getTomorrow(today);
var count = 0;
var countz = 1;
var newMax = 0;
while(count < max) {
if (includeDate(nextAvailable) ) {
count++;
}
if (includeDate(nextAvailable) ) {
countz++;
}
newMax++;
nextAvailable = getTomorrow(nextAvailable);
}
return newMax;
})
(3)
});
http://jsfiddle.net/3o1dmvw5/96/
This below should be the solution. The problem with your code is that you forgot to verify the date string to see if it is in the array or not using your includeDate() function. Thus, your includeDate() function allow that date, while maxDate didn't allow that date.
Also, you can also use array.indexOf() instead of jQuery's inArray. I am pretty sure that native array.indexOf() probably is faster.
Besides that, I modify your maxDate() function a little bit. It now look less confusing. I used window onload so that I can debug the code easy. You can just take that out.
For my version, when it come to verify the days, beforeShowDay and includeDate does the same thing. Thus, I edited beforeShowDay() to just return the value from the function includeDate().
Also, you should change the input selector to an ID(#) or Class(.). Otherwise, your datepicker will proc on all input fields.
Also, I modified you includeDate() function. There isn't a day 7 as 0 - 6 = Sunday - Saturday.
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.min.js"></script>
<script src="//code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script>
var array = ["02-12-2019"]
//new
function includeDate(date) {
var dateStr = jQuery.datepicker.formatDate('dd-mm-yy', date);
// Date 0 = Sunday & 6 = Saturday
return date.getDay() !== 0 && array.indexOf(dateStr) === -1;
}
function getTomorrow(date) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1);
}
//prev
window.onload = function(){
$('input').datepicker(
{
defaultDate: "+1d",
inline: true,
showOtherMonths: true,
changeMonth: true,
selectOtherMonths: true,
required: true,
showOn: "focus",
numberOfMonths: 1,
minDate: 1,
beforeShowDay: function(date) {
return [includeDate(date)];
},
maxDate: (function(max) {
// Next available is today at first.
var nextAvailable = new Date();
var count = 0;
var extra = 0;
while(count < max) {
/*
* Next available is here so that getTomorrow does not need
* to run an extra time when the loop is completed.
*
*/
nextAvailable = getTomorrow(nextAvailable);
// If the day is not available then we need to add an extra day.
if ( !includeDate(nextAvailable) ) {
extra++;
// Else we just increase the count.
} else {
count++;
}
}
// Return max + extra.
return max + extra;
})
(3)
});
};
</script>
<p>Date: <input type="text" id="datepicker"></p>

How do I set the end date of a jQuery ui datepicker calendar to the users start date please?

I'm new to Javascript and would appreciate some help with my jQuery datepicker code, please. I've spent ages looking on here and trying to fix my code but just can't seem to get it to work...I would like the user to select the start date and then when they click on the end date, they can only choose dates from the start date onwards. I also need to calculate the number of days (but I have done this and its working).
Heres the script code:
function showDays() {
var start = $('#startDate').datepicker('getDate');
var end = $('#endDate').datepicker('getDate');
if (!start || !end) return;
var days = (end - start) / 1000 / 60 / 60 / 24;
$('#noDays').val(days);
if (days > 15) {
alert ( "The maximum amount of nights is 15. Please select dates within this range." )
$( '#endDate' ).focus();
}
document.getElementById("p__myTrip--days").innerHTML = days;
}
$("#startDate").datepicker({
dateFormat: 'dd-mm-yy',
minDate: 0,
changeMonth: true,
onSelect: showDays,
onClose: function( selectedDate ) {
var dParts = selectedDate.split('-');
var in30Days = new Date(dParts[2] + '/' +
dParts[1] + '/' +
(+dParts[0] + 30)
);
$("#endDate").datepicker("option", "minDate");
}
});
$("#endDate").datepicker({
minDate: 1,
changeMonth: true,
dateFormat: 'dd-mm-yy',
setDate: '#startDate',
onSelect: showDays
});
Any help would be appreciated! Thank you! :)
You can try this:
$("#endDate").datepicker({
minDate: 1,
changeMonth: true,
dateFormat: 'dd-mm-yy',
setDate: '#startDate',
endDate: '#endDate',
onSelect: showDays
});

jquery datepicker disable all dates accept one

Thank you for your help to people.
Look, I'm not a jQuery programmer and I stole alredy finished version of calendar, but still have something to change:
var enabledDays = ["6-1-2013", "7-1-2013", "8-1-2013", "9-1-2013", "10-1-2013", "11-1-2013"];
function nationalDays(date) {
var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
for (i = 0; i < enabledDays.length; i++) {
if($.inArray((m+1) + '-' + d + '-' + y,enabledDays) != -1 || new Date() > date) {
return [true];
}
}
return [false];
}
$(function(){
$.datepicker.setDefaults($.extend($.datepicker.regional["ru"]));
$("#datepicker1, #datepicker2, #datepicker3").datepicker({dateFormat: "yy-mm-dd",
duration: "normal",
numberOfMonths: [ 1, 2 ],
constrainInput: true,
beforeShowDay: nationalDays});
});
This is regular datepicker which you can find all over in internet. I have var enabledDays which specifying the particulat month-date-year I need just set first date if each month in calendar activated and other disable. How can I do this guys. Thank you.
Does this plugin work for your needs?
http://multidatespickr.sourceforge.net/

Disable holiday, sundays and past dates inside jQuery UI datepicker

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

Categories

Resources