jQuery datepicker的最大日期

时间:2012-08-23 15:02:43

标签: jquery datepicker

任何人都可以看到这里出了什么问题,我试图从计算日期的php变量设置datepicker的最大日期。

我用过:

$ac_start_date = 01-08-2012
$ac_start_date = 20120801

但两者都没有。

 $(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;
  });
}

修改 修改版的答案

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'
});

1 个答案:

答案 0 :(得分:2)

这样做可以更容易:

updated(8/29) jsFiddle DEMO

明天将minDate设为+ 1d(+1天)... 解析即将进入Days的日期,查看今天的天数,并将其设置为maxDate

var $ac_start_date = '08-27-2012',
    $ac_start_date_flip = '2012-08-27',
    $ac_start_parsed = Date.parse($ac_start_date),
    _today = new Date().getTime();

// For Opera and older winXP IE n such
if (isNaN($ac_start_parsed)) { 
    $ac_start_parsed  = Date.parse($ac_start_date_flip);
}

var _aDayinMS = 1000 * 60 * 60 * 24; 

// Calculate the difference in milliseconds
var difference_ms = Math.abs($ac_start_parsed - _today);

// Convert back to days and return
var DAY_DIFFERENCE = Math.round(difference_ms/_aDayinMS); 

$('.datepicker').datepicker({ 
    changeMonth: true,
    changeYear: true,
    yearRange: '0:+100',
    minDate: '+1d',
    maxDate: '+' + DAY_DIFFERENCE + 'd'
});

瞧瞧:)