2

谁能看到这里出了什么问题,我正在尝试从计算日期的 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'
});
4

1 回答 1

2

这可以更容易地完成,如下所示:

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

瞧 :)

于 2012-08-23T15:10:48.330 回答