Can anyone see what is going wrong here, i am trying to set the maximum date for datepicker from a php variable where the date is calculated.
I have used:
$ac_start_date = 01-08-2012
$ac_start_date = 20120801
But neither are working.
$(document).ready(dialogForms);
function dialogForms() {
$('a.menubutton').click(function() {
var a = $(this);
$.get(a.attr('href'),function(resp){
var dialog = $('<div>').attr('id','formDialog').html($(resp).find('form:first').parent('div').html());
$('body').append(dialog);
dialog.find(':submit').hide();
dialog.dialog({
title: a.attr('title') ? a.attr('title') : '',
modal: true,
buttons: {
'Save': function() {
submitFormWithAjax($(this).find('form'));
location.reload();
$(this).dialog('close');
},
'Cancel': function() {$(this).dialog('close');}
},
close: function() {$(this).remove();},
width: 600,
height: 500,
show: "fade",
hide: "fade"
});
// do initialization here
$("#startdate").datepicker({
dateFormat: 'dd-mm-yy',
minDate: 'tomorrow',
maxDate: new Date("<?php echo $ac_end_date; ?>")
});
// do initialization here
$("#enddate").datepicker({
dateFormat: 'dd-mm-yy',
minDate: 'tomorrow',
maxDate: new Date("<?php echo $ac_end_date; ?>")
});
}, 'html');
return false;
});
}
EDIT
Modified version of Answer
var ac_end_date = '07-31-2012'; // you want this to start out as a string
var ac_end_parsed = Date.parse(ac_end_date); // parse into milliseconds
var today = new Date().getTime(); // baseline
console.log(ac_end_date);
console.log(ac_end_parsed);
console.log(today);
var aDayinMS = 1000 * 60 * 60 * 24;
console.log(aDayinMS);
// Calculate the difference in milliseconds
var difference_ms = Math.abs(today - ac_end_parsed);
console.log(difference_ms);
// Convert back to days and return
var DAY_DIFFERENCE = Math.round(difference_ms/aDayinMS);
console.log(DAY_DIFFERENCE);
// do initialization here
$("#startdate").datepicker({
changeMonth: true,
changeYear: true,
yearRange: '0:+100',
minDate: '+1d',
maxDate: '+' + DAY_DIFFERENCE + 'd'
});
// do initialization here
$("#enddate").datepicker({
changeMonth: true,
changeYear: true,
yearRange: '0:+100',
minDate: '+1d',
maxDate: '+' + DAY_DIFFERENCE + 'd'
});
This can be done even easier like so:
updated(8/29) jsFiddle DEMO
Simply set minDate as +1d (+1 day) for tomorrow…
Parse your date that’s coming in just for Days, see how many Days from today it is, and set that as the
maxDate.And voila 🙂