Jquery UI datepicker setdate not working for one datepicker - jquery-ui

I have 2 datepickers one for dateend and another for datestart.
I have a button in the html when you click it a dialog-form is going to appear.
then a ask data and populate the elements in the modal form. the modal form has date end and date start. but it seems the datestart is the only one populated. pls help. i really dont know what is wrong, stuck for more than 4days
<script type="text/javascript">
$(document).ready(function () {
$("#dialog-form").dialog({
autoOpen: false,
show: "drop", //explode
hide: "clip",
autoResize: true,
height: '500',
width: 'auto',
draggable: false,
modal: true,
resizable: false
});
$('input[type="button"]').click(function () {
var buttonHolder = $('input[type="button"]');
$("#dialog-form").dialog("open");
$("#timeStart").timepicker({});
$("#dateStart").datepicker({
defaultDate: "+1w",
dateFormat: "yy/mm/d",
changeMonth: true,
changeYear: true,
numberOfMonths: 3,
minDate: new Date(), // min date should be date of today or the date which he set as startdate
onClose: function () { //selectedDate should be value. i think. problem lies in the selected.
$("#dateEnd").datepicker("option", "minDate", $(this).val());
}
});
$("#dateEnd").datepicker({
defaultDate: "+1w",
dateFormat: "yy/mm/d",
changeMonth: true,
changeYear: true,
numberOfMonths: 3,
onClose: function () {
$("#dateStart").datepicker("option", "maxDate", $(this).val()); //$(this).val()
}
});
$.ajax({
type: "POST",
dataType: "json",
url: "<?php echo site_url('ajax/getEventOfModal'); ?>",
data: {
id: $(this).attr('id'),
username: "<?php echo $this->session->userdata('email'); ?>"
},
success: function (data) {
$("#event_id").attr('value', buttonHolder.attr('id'));
$("#event_name").attr('value', data['name']);
$("#event_loc").text(data['loc']);
$("#event_desc").text(data['desc']);
$("#timeStart").timepicker('setTime', data['timeStart']);
var queryDate = data['dateStart'],
dateParts = queryDate.match(/(\d+)/g)
realDate = new Date(dateParts[0], dateParts[1] - 1, dateParts[2]); // months are 0-based!
$('#dateStart').datepicker('setDate', realDate);
var queryDate2 = data['dateEnd'],
dateParts2 = queryDate2.match(/(\d+)/g)
realDate2 = new Date(dateParts2[0], dateParts2[1] - 1, dateParts2[2]); // months are 0-based!
$('#dateEnd').datepicker('setDate', realDate2);
}
});
});
});
</script>

Try removing the defaultdate from the jquery datepicker.

Related

How in the JQ UI Datepicker to show always the date in the input field

I implemented in my app a JQ Datepicker calendar on its default version, but I cannot understand how to show in the input field always the date the today date to be precise. Now when I open the page where is the datepicker, the input field is blank until I select the date I need.
What I would like to see is that the date is always visible on the input filed if that possible.
My Datepicker:
//Date Picker
$("*[data-behavior~=shipping-date-input]").datepicker({
onSelect: function() {
digest = cargoflux.generateUUID();
cargoflux.resetCalculatedFields();
clearTimeout(timeOut);
timeOut = setTimeout(function() {
if (cargoflux.validInput()) {
cargoflux.resetCalculatedFields();
cargoflux.getAvailableCarrierProducts();
}
}, 1500);
},
showOtherMonths: true,
selectOtherMonths: true,
changeMonth: true,
changeYear: true,
altField: "#recorded-at-alt",
dateFormat: "yy-mm-dd",
minDate: "-1Y",
maxDate: "+1Y"
});
Screenshot of what I mean as you see the input is blank in his initial state:
To populate the value of the text field, you can use $.datepicker.formatDate( format, date, options ) feature. here is an example:
$(function() {
var dtFt = "yy-mm-dd";
$("#datepicker").datepicker({
onSelect: function() {
/*digest = cargoflux.generateUUID();
cargoflux.resetCalculatedFields();
clearTimeout(timeOut);
timeOut = setTimeout(function() {
if (cargoflux.validInput()) {
cargoflux.resetCalculatedFields();
cargoflux.getAvailableCarrierProducts();
}
}, 1500);
*/
},
showOtherMonths: true,
selectOtherMonths: true,
changeMonth: true,
changeYear: true,
//altField: "#recorded-at-alt",
dateFormat: dtFt,
minDate: "-1Y",
maxDate: "+1Y"
}).val($.datepicker.formatDate(dtFt, new Date()));
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<p>Date: <input type="text" id="datepicker"></p>
As you can see, we populate the text field value, $("#datepicker").val();.
Hope that helps.

JQuery UI date picker end date 1 day after start date

So i am trying to restrict the user from selecting the check-out date
to be the same as the check-in date using jQuery UI Date range picker
(http://jqueryui.com/datepicker/#date-range). I have it where they are
not able to select before the check-in date but as of right now the
check-in date and check-out date can be the same files.
This is the jquery
$(function() {
$( "#check-in" ).datepicker({
minDate: 0,
dateFormat: "yy-mm-dd",
changeMonth: true,
numberOfMonths: 2,
changeYear: true,
onClose: function( selectedDate, inst ) {
$( "#check-out" ).datepicker( "option", "minDate", selectedDate);
}
});
$( "#check-out" ).datepicker({
minDate: "+1D",
dateFormat: "yy-mm-dd",
changeMonth: true,
numberOfMonths: 2,
changeYear: true,
onClose: function( selectedDate, inst ) {
$( "#check-in" ).datepicker( "option", "maxDate", selectedDate +"+1D");
}
});
});
This is the HTML
<div class="formInput">
<label for="check-in">Check-in:</label>
<input type="text" id="check-in" name="check-in" value="yyyy/mm/dd" size="30" class="textInput">
</div>
<div class="formInput">
<label for="check-out">Check-out:</label>
<input type="text" id="check-out" name="check-out" value="yyyy/mm/dd" size="30" class="textInput">
</div>
What I want is the check out date to default to 1 day after the check in date every time the check-in date is selected. Thank you in advance
You can't add days using this code
$( "#check-in" ).datepicker( "option", "maxDate", selectedDate +"+1D")
Try this instead:
onClose: function( selectedDate, inst ) {
var maxDate = new Date(Date.parse(selectedDate));
maxDate.setDate(maxDate.getDate() - 1);
$( "#check-in" ).datepicker( "option", "maxDate", maxDate);
}
Here's the fiddle: http://jsfiddle.net/RxTax/1/
change minDate.setDate(maxDate.getDate() + 1);
to minDate.setDate(minDate.getDate() + 1);
the fact is that is just working with dateFormat: "yy-mm-dd" and not with other dateFormat as regional fr or alike.
I have written a work-around for other dateFormats using alternate fields. I tested on IE chrome and Firefox and works quite good.
function resetFrom() {
var altcheck-in = document.getElementById("altcheck-in");
var check-in= document.getElementById("check-in");
altcheck-in.value = "";
if (altcheck-in.value == "") {
check-in.value = "";
$("#check-out").datepicker("destroy");
$("#check-out").datepicker({
minDate: "+1D",
dateFormat: "yy-mm-dd",
altFormat: "dd-mm-yy",
altField: "#altcheck-out",
changeMonth: true,
numberOfMonths: 1,
changeYear: true,
showOn: "button",
buttonImage: "/images/calendar.gif",
buttonImageOnly: true,
buttonText: "Select date",
onClose: function (selectedDate) {
if (selectedDate != "") {
var maxDate = new Date(Date.parse(selectedDate));
maxDate.setDate(maxDate.getDate() - 1);
$("#check-in").datepicker("option", "maxDate", maxDate);
}
}
});
}
}
function resetTo() {
var altcheck-out = document.getElementById("altcheck-out");
var check-out = document.getElementById("check-out");
altTo.value = "";
if (altcheck-out.value == "") {
to.value = "";
$("#check-in").datepicker("destroy");
$("#check-in").datepicker({
dateFormat: "yy-mm-dd",
altFormat: "dd-mm-yy",
altField: "#altcheck-in",
minDate: 0,
changeMonth: true,
numberOfMonths: 1,
changeYear: true,
showOn: "button",
buttonImage: "/images/calendar.gif",
buttonImageOnly: true,
buttonText: "Select date",
onClose: function (selectedDate) {
if (selectedDate != "") {
var minDate = new Date(Date.parse(selectedDate));
minDate.setDate(minDate.getDate() + 1);
$("#check-out").datepicker("option", "minDate", minDate);
}
}
});
}
}
$(function () {
$("#check-in").datepicker({
dateFormat: "yy-mm-dd",
altFormat: "dd-mm-yy",
altField: "#altcheck-in",
minDate: 0,
changeMonth: true,
numberOfMonths: 1,
changeYear: true,
showOn: "button",
buttonImage: "/images/calendar.gif",
buttonImageOnly: true,
buttonText: "Select date",
onClose: function (selectedDate) {
if (selectedDate != "") {
var minDate = new Date(Date.parse(selectedDate));
minDate.setDate(minDate.getDate() + 1);
$("#check-out").datepicker("option", "minDate", minDate);
}
}
});
$("#check-out").datepicker({
minDate: "+1D",
dateFormat: "yy-mm-dd",
altFormat: "dd-mm-yy",
altField: "#altcheck-out",
changeMonth: true,
numberOfMonths: 1,
changeYear: true,
showOn: "button",
buttonImage: "/images/calendar.gif",
buttonImageOnly: true,
buttonText: "Select date",
onClose: function (selectedDate) {
if (selectedDate != "") {
var maxDate = new Date(Date.parse(selectedDate));
maxDate.setDate(maxDate.getDate() - 1);
$("#check-in").datepicker("option", "maxDate", maxDate);
}
}
});
});
<input name="altcheck-in" type="text" id="altcheck-in" onchange="resetFrom();" style="width:250px;" />
<input name="check-in" type="text" id="check-in" style="display: none" />
<input name="altcheck-out" type="text" id="altcheck-out" onchange="resetTo();" style="width:250px;" />
<input name="check-out" type="text" id="check-out" style="display: none" />
JQuery UI date picker end date 1 day after start date
http://jsfiddle.net/wskhcgdq/1/
You can't add days using this code
$( "#check-in" ).datepicker( "option", "maxDate", selectedDate +"+1D")
Try this instead:
$(function() {
$( "#check-in" ).datepicker({
minDate: 0,
dateFormat: "yy-mm-dd",
changeMonth: true,
numberOfMonths: 2,
changeYear: true,
onClose: function( selectedDate, inst ) {
var minDate = new Date(Date.parse(selectedDate));
minDate.setDate(maxDate.getDate() + 1);
$( "#check-out" ).datepicker( "option", "minDate", minDate);
}
});
$( "#check-out" ).datepicker({
minDate: "+1D",
dateFormat: "yy-mm-dd",
changeMonth: true,
numberOfMonths: 2,
changeYear: true,
onClose: function( selectedDate, inst ) {
var maxDate = new Date(Date.parse(selectedDate));
maxDate.setDate(maxDate.getDate() - 1);
$( "#check-in" ).datepicker( "option", "maxDate", maxDate);
}
});
});

change the second datepicker's minDate depending the first one

I have two datePickers in the same page and I wanna change the second datepicker's minDate depending the first one. Here is my code:
$("#datepickerDepart" ).datepicker({
dateFormat: "mm-dd-yy",
minDate: 0,
onSelect: function(dateText, inst) {
var m = dateText.substring(0,2);
var d = dateText.substring(3,5);
var y = dateText.substring(6,10);
console.log(d);
console.log(m);
console.log(y);
var newDate = new Date(y,m-1,d);
console.log(newDate);
$('#datepickerReturn').val("");
$('#datepickerReturn').datepicker({
dateFormat: "mm-dd-yy",
minDate: newDate
})
}
});
But I have a question, the first time I select a date in the first datePicker, the minDate of second one will be set according to this, but when I reselect the first one, the second datePicker's minDate won't change anymore, it will remain. I don't know why. Please help!!
I just wanna the minDate of the second one will change as long as the selected date of the first one change.
You are updating the second datepicker incorrectly. I am assuming you have already initialized the second one previously, although your code posted doesn't appear to. You need to use the option syntax when updating it, instead of the constructor.
http://jqueryui.com/demos/datepicker/#option-minDate
$('#datepickerReturn').val("");
$('#datepickerReturn').datepicker({'option','minDate', newDate});
Full code:
$('#datepickerReturn').datepicker({
dateFormat: "mm-dd-yy",
minDate: 0
});
$("#datepickerDepart" ).datepicker({
dateFormat: "mm-dd-yy",
minDate: 0,
onSelect: function(dateText, inst) {
var m = dateText.substring(0,2);
var d = dateText.substring(3,5);
var y = dateText.substring(6,10);
console.log(d);
console.log(m);
console.log(y);
var newDate = new Date(y,m-1,d);
console.log(newDate);
$('#datepickerReturn').val("");
$('#datepickerReturn').datepicker({'option','minDate', newDate});
}
});
$("#strStartDate").datepicker({
dateFormat: 'dd/mm/yy',
constrainInput: true,
firstDay: 1,
hideIfNoPrevNext: true,
onSelect: function(){
if ($("#strStartDate").val() > $("#strEndDate").val()){
$("#strEndDate").val($("#strStartDate").val());
}
}
});
$("#strEndDate").datepicker({
dateFormat: 'dd/mm/yy',
constrainInput: true,
firstDay: 1,
hideIfNoPrevNext: true,
beforeShow: function (input, inst) {
inst.settings.minDate = $("#strStartDate").val();
}
});
You can update secodn datepicker's mindate with following code :
$( "#ddate" ).datepicker({
showOn: "button",
buttonImage: base_path+"img/date_picker.png",
buttonImageOnly: true,
onSelect: function (selectedDate) {
$("#rdate").datepicker("option", "minDate", selectedDate);
},
minDate: 'today'
});
$( "#rdate" ).datepicker({
showOn: "button",
buttonImage: base_path+"img/date_picker.png",
buttonImageOnly: true,
minDate: 'today'
});

Jquery datepicker throwing too much recursion error

Im trying to modify the jquery datepicker with done button as clear button. but it is throwing too much recursion error...
my code is,
function datePickerToCompareTwoFields(){
var dformat = document.getElementById('dateFormat').value;
$( '#popup_container, #popup_container1' ).datepicker({
beforeShow: customRange,
changeMonth: true,
changeYear: true,
closeText: 'Clear',
dateFormat: dformat,
numberOfMonths: 2,
selectOtherMonths: true,
showButtonPanel: true,
showOtherMonths: true
});
$.datepicker._generateHTML_Old = $.datepicker._generateHTML;
$.datepicker._generateHTML = function(inst) {
res = this._generateHTML_Old(inst);
res = res.replace("_hideDatepicker()","_clearDate('#"+inst.id+"')");
return res;
}
}

Closing jQuery Datepicker When Closing jQuery Dialog

First time here, and more of a web designer than programmer, so be gentle! ;o) Anyway, as the title suggests, I have a dialog window that's opened and within that, a datepicker. What I want it that, if the user opens the datepicker, and then clicks the dialog window's close button, the datepicker is also closed.
Here's what I've got at present:
<!--// Modal Windows -->
$.ui.dialog.defaults.bgiframe = true;
$(function() {
$('#advanced_search').dialog({
autoOpen: false,
width: 600,
height: 400,
modal: true,
resizable: false,
draggable: false,
title: 'Advanced Search',
});
$('.adv_search').click(function() {
$('#advanced_search').dialog('open');
});
});
<!--// Date Picker -->
$("#advanced_search .start_date").datepicker({
dateFormat: 'dd/mm/yy',
showButtonPanel: true,
duration: 0,
constrainInput: true,
showOn: 'button',
buttonImage: '/img/icons/50.png',
buttonImageOnly: true
});
$("#advanced_search .end_date").datepicker({
dateFormat: 'dd/mm/yy',
showButtonPanel: true,
duration: 0,
constrainInput: true,
showOn: 'button',
buttonImage: '/img/icons/50.png',
buttonImageOnly: true
});
But I'm a bit flummoxed as to how to do this. Anyone got any advice? It'd be much appreciated!
Thanks
Phil
Add the close callback to your dialog like this:
$(function() {
$('#advanced_search').dialog({
autoOpen: false,
width: 600,
height: 400,
modal: true,
resizable: false,
draggable: false,
title: 'Advanced Search',
close: function() {
$("#advanced_search .start_date").datepicker('hide');
$("#advanced_search .end_date").datepicker('hide');
}
});
$('.adv_search').click(function() {
$('#advanced_search').dialog('open');
});
});
Here's an all-inclusive approach that's slightly better, simpler selectors and the date pickers aren't created until the dialog is actually opened, so if a user never goes into it, less script running:
$(function() {
$('#advanced_search').dialog({
autoOpen: false,
width: 600,
height: 400,
modal: true,
resizable: false,
draggable: false,
title: 'Advanced Search',
close: function() {
$("#advanced_search .start_date").datepicker('hide');
$("#advanced_search .end_date").datepicker('hide');
},
open: function(event, ui) {
$(".start_date, .end_date", ui).datepicker({
dateFormat: 'dd/mm/yy',
showButtonPanel: true,
duration: 0,
constrainInput: true,
showOn: 'button',
buttonImage: '/img/icons/50.png',
buttonImageOnly: true
});
}
});
$('.adv_search').click(function() {
$('#advanced_search').dialog('open');
});
});

Resources