gpt4 book ai didi

jquery - jQuery 日期选择器的最大日期

转载 作者:行者123 更新时间:2023-12-01 04:19:35 25 4
gpt4 key购买 nike

任何人都可以看到这里出了什么问题,我正在尝试从计算日期的 php 变量中设置日期选择器的最大日期。

我用过:

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

最佳答案

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

updated(8/29) jsFiddle DEMO

只需将明天的 minDate 设置为 +1d(+1 天)...解析仅包含天数的日期,查看距今天有多少天,并将其设置为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'
});

瞧:)

关于jquery - jQuery 日期选择器的最大日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12094668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com