jqueryui Datepicker,今年的日期范围限制

时间:2017-04-23 12:52:02

标签: jquery jquery-ui datepicker

我花了最后几个小时来修补和寻找没有运气的答案,但我相信这对一些人来说相当简单。

我希望在“日期范围”模式下使用jqueryui的Datepicker,但我想限制只能选择今年的日期。我还想将日期格式从mm / dd / yy更改为dd / mm / yy。

我将发布到原始代码,因为我发布任何失败的努力都没有任何价值。

希望有人可以帮助我,谢谢。

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Select a Date Range</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
var dateFormat = "mm/dd/yy",
from = $( "#from" )
.datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 3
})
.on( "change", function() {
to.datepicker( "option", "minDate", getDate( this ) );
}),
to = $( "#to" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 3
})
.on( "change", function() {
from.datepicker( "option", "maxDate", getDate( this ) );
});

function getDate( element ) {
var date;
try {
date = $.datepicker.parseDate( dateFormat, element.value );
} catch( error ) {
date = null;
}

return date;
}
} );
</script>
</head>
<body>

1 个答案:

答案 0 :(得分:0)

根据您的需要调整以下代码:

$(function() {
    var year = (new Date).getFullYear();
    $( "#from" ).datepicker({
        minDate: new Date(year, 0, 1),
        maxDate: new Date(year, 11, 31)
    });
});

var from = $('#from').datepicker({ dateFormat: 'dd-mm-yy' }).val();

原帖:herehere

对于您的具体示例,可以像这样应用(未经测试我没有您的完整代码/ css):

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>jQuery UI Datepicker - Select a Date Range</title>

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<script>
    $( function() {
        var year = (new Date).getFullYear();

        from = $( "#from" ).datepicker({
            minDate: new Date(year, 0, 1),
            maxDate: new Date(year, 11, 31),
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 3
        })
        .on( "change", function() {
            $( "#to" ).datepicker( "option", "minDate", new Date($("#from").datepicker().val() )); // You set the min date of `to` to be the selected date here
        }),

        to = $( "#to" ).datepicker({
            maxDate: new Date(year, 11, 31), // minDate has already been set
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 3
        });

        function getDate( element ) {
            var date;
            try {
                date = $(element).datepicker({ dateFormat: 'dd-mm-yy' }).val();
            } catch( error ) {
                date = null;
            }
            return date;
        }
    });
</script>
</head>

<body>

<!-- dummy datepickers -->
<div id="from"></div> 
<div id="to"></div> 

<!-- It is a good practice to import scripts at the end of your page, helps to render quicker the DOM -->
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

</body>
</html>

希望它有所帮助;)