jQuery 日期选择器限制当前年份
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18130249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
datepicker limit current year
提问by Alessandro Minoccheri
I have a little problem with datepicker, I'd want that a user with arrows can only select a date between the first day of the first month and the last day of the month, but it would be dynamic because if we are in 2014 I have to see only year of 2014.
我对日期选择器有一个小问题,我希望带箭头的用户只能选择第一个月的第一天和该月的最后一天之间的日期,但它是动态的,因为如果我们在 2014 年我必须只看到 2014 年。
I have tried in this mode but with arrows I can go to 2012 or 2014 for example:
我已经尝试过这种模式,但是使用箭头我可以去 2012 年或 2014 年,例如:
$('#check-in').datepicker({ dateFormat: 'dd-mm', changeYear: false, yearRange: "-0:+0", stepYears: 0 });
回答by Raidri supports Monica
回答by Michel Ayres
Just use yearRange
like this:
只需yearRange
像这样使用:
datepick({yearRange: new Date().getFullYear() + ':' + new Date().getFullYear()});
回答by Sasanka Panguluri
Why not mention the yearRange to be currentyear : currentyear?
为什么不提到 yearRange 是 currentyear : currentyear?
You can do this: after
你可以这样做:之后
$(document).ready(function(){
var curyear = $("#year").text( (new Date).getFullYear() );
Inside your datepicker,
yearRange: "+curyear+":"+curyear+"
在您的日期选择器中,
yearRange: "+curyear+":"+curyear+"
回答by cssyphus
This will work for you:
这对你有用:
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(document).ready(function() {
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var fSDate = new Date(y, m, 1);
var fEDate = new Date(y, m + 1, 0);
$.datepicker.setDefaults({
changeMonth: false,
changeYear: false,
showOtherMonths: false,
yearRange: '-0:+0',
dateFormat: 'yy-mm-dd',
defaultDate: +0,
numberOfMonths: 1,
minDate: fSDate,
maxDate: fEDate,
showAnim: 'fadeIn',
showButtonPanel: false
});
$('#date_start').datepicker();
}); //END document.ready()
</script>
</head>
<body>
<input type="text" id="date_start">
</body>
</html>