最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I change my date picker into a year only picker? - Stack Overflow

programmeradmin7浏览0评论

I want to change to year picker calendar only in a dynamic input fields.The calender should show only year values Can any one help me please.

Here is the javascript code.

$('body').on('focus',".datepicker", function(){

    if( $(this).hasClass('hasDatepicker') === false )  {
        $(this).datepicker();
    }

});

I want to change to year picker calendar only in a dynamic input fields.The calender should show only year values Can any one help me please.

Here is the javascript code.

$('body').on('focus',".datepicker", function(){

    if( $(this).hasClass('hasDatepicker') === false )  {
        $(this).datepicker();
    }

});
Share Improve this question edited Jan 27, 2018 at 8:30 n.ssendawula asked Aug 29, 2017 at 6:38 n.ssendawulan.ssendawula 332 silver badges9 bronze badges 5
  • 1 which datepicker library are you using ? – shyammakwana.me Commented Aug 29, 2017 at 6:42
  • 2 The date picker would be a massive overhead for this task. Simply make a drop down with a list of numbers from year 19xx to 20xx – Herr Derb Commented Aug 29, 2017 at 6:42
  • stackoverflow./questions/13528623/… – Iti Tyagi Commented Aug 29, 2017 at 6:45
  • its like,it works very well but it displays dates, month, and year calendar.but for me i wanted to display years only.i will be so glad when you help me on that please. – n.ssendawula Commented Aug 29, 2017 at 6:47
  • Possible duplicate of jQuery UI DatePicker to show year only – julianstark999 Commented Aug 29, 2017 at 6:47
Add a ment  | 

3 Answers 3

Reset to default 2

You can try this one. And see if it will help your problem.

HTML:

<select name="yearpicker" id="yearpicker"></select>

JS:

var startYear = 1800;
for (i = new Date().getFullYear(); i > startYear; i--)
{
  $('#yearpicker').append($('<option />').val(i).html(i));
}

Here is a jsfiddle link

 $('body').on('focus',".datepicker", function(){
    if( $(this).hasClass('hasDatepicker') === false )  {
        $(this).datepicker({
            minViewMode: 2,
            format: 'yyyy'
        });
    }
});
<script src="https://code.jquery./jquery-2.2.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/css/bootstrap.min.css" />
<link href="https://cdnjs.cloudflare./ajax/libs/bootstrap-datepicker/1.5.0/css/bootstrap-datepicker.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare./ajax/libs/bootstrap-datepicker/1.5.0/js/bootstrap-datepicker.js"></script>
<input type="text" class="datepicker" />

I assume it is bootstrap datepicker (Link)

I would not use a datepicker if you want to select a year only. Use a select for that. However, you can modifiy the datepicker (since you didn't mention which one you are using I assume jQuery UI) to only return the year.

Have a look at this fiddle:

$(function() { 
    $(".datepicker").datepicker({ dateFormat: 'yy' });
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://code.jquery./ui/1.9.1/themes/base/jquery-ui.css" />
<script src="https://code.jquery./ui/1.9.1/jquery-ui.js"></script>

<input type="text" class="datepicker"/>

发布评论

评论列表(0)

  1. 暂无评论