I have the following Load JavaScript JS code in the Setup portion of a CFv5 form:
Date.prototype.dbFormat = function() {
var yyyy = this.getFullYear().toString();
// getMonth() is zero-based
var mm = (this.getMonth()+1).toString();
var dd = this.getDate().toString();
return yyyy + "-" + (mm[1]?mm:"0"+mm[0]) +
"-" + (dd[1]?dd:"0"+dd[0]);
};
function setEndDate() {
var d = new Date(jQuery("#start_date").val());
d.setDate(d.getDate() + 1);
jQuery("#end_date").val(d.dbFormat());
}
setEndDate is the value of On date selected in the Datepicker field configuration.
On setting my Start Date field, the End Date field is updated as follows:
[attachment=0]Datepicker JS Off-By-One Issue.PNG[/attachment]
Although the Datepicker chooses 2015-06-12 and properly updates Start Date, the incremented date then placed in End Date show the same date. And if I actually do not add 1 to the date, but simply have it use the Date created by new Date(jQuery("#start_date").val()) the date that ends up in End Date is one day less!
Maybe this is just my lack of experience with JavaScript Dates, but this is really wrong and strange.
I have seen elsewhere, where it is recommended to use functions on a jQuery datepicker to parse and manipulate Dates, but I am pretty new to jQuery and do not have a clue as the appropriate way to get the date properly incremented by a defined number of days.
Can you point me to or provide any examples of the best and most appropriate ways to do this with the CFv5 datetimepicker fields?
Date.prototype.dbFormat = function() {
var yyyy = this.getFullYear().toString();
// getMonth() is zero-based
var mm = (this.getMonth()+1).toString();
var dd = this.getDate().toString();
return yyyy + "-" + (mm[1]?mm:"0"+mm[0]) +
"-" + (dd[1]?dd:"0"+dd[0]);
};
function setEndDate() {
var d = new Date(jQuery("#start_date").val());
d.setDate(d.getDate() + 1);
jQuery("#end_date").val(d.dbFormat());
}
setEndDate is the value of On date selected in the Datepicker field configuration.
On setting my Start Date field, the End Date field is updated as follows:
[attachment=0]Datepicker JS Off-By-One Issue.PNG[/attachment]
Although the Datepicker chooses 2015-06-12 and properly updates Start Date, the incremented date then placed in End Date show the same date. And if I actually do not add 1 to the date, but simply have it use the Date created by new Date(jQuery("#start_date").val()) the date that ends up in End Date is one day less!
Maybe this is just my lack of experience with JavaScript Dates, but this is really wrong and strange.
I have seen elsewhere, where it is recommended to use functions on a jQuery datepicker to parse and manipulate Dates, but I am pretty new to jQuery and do not have a clue as the appropriate way to get the date properly incremented by a defined number of days.
Can you point me to or provide any examples of the best and most appropriate ways to do this with the CFv5 datetimepicker fields?