HTML5 input type show only years

Ramesh S picture Ramesh S · Sep 27, 2018 · Viewed 11k times · Source

Maybe this question already asked but that not solve my problem.

This is my query in input box i want to show only years like this

enter image description here

Thanks in advance

Note: Already this qustions asked but it not solve my issue

Answer

Rhalp Darren Cabrera picture Rhalp Darren Cabrera · Jun 20, 2019

How to Resolve input year only <input date="year">

You can use this snippet.

PHP Version:

  <select class="form-control" name="startyear">
   <?php
   for ($year = (int)date('Y'); 1900 <= $year; $year--): ?>
     <option value="<?=$year;?>"><?=$year;?></option>
   <?php endfor; ?>
  </select>

JavaScript Version:

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

    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script type="text/javascript">
    let startYear = 1800;
    let endYear = new Date().getFullYear();
    for (i = endYear; i > startYear; i--)
    {
      $('#yearpicker').append($('<option />').val(i).html(i));
    }
    </script>