I have a field that is required to collect a year from the user (i.e. a date with a year resolution - for ease of storage I'd prefer to store an actual date value and not a number).
I would like to use the date input UI supported by modern browsers or webshims because its nice and plays well with modern platforms. I couldn't figure out how to get the UI to display only the year. Any ideas?
If there is no platform support, ideally I would like to have a UI something like you can see here if you click "Preview" and then click the top of the widget a couple of times, except in reverse: when you first click the input you get a widget with a list of decades, then you drill down to choose the year, then the UI closes.
<!--This yearpicker development from Zlatko Borojevic_x000D_
html elemnts can generate with java function_x000D_
and then declare as custom type for easy use in all html documents _x000D_
For this version for implementacion in your document can use:_x000D_
1. Save this code for example: "yearonly.html"_x000D_
2. creaate one div with id="yearonly"_x000D_
3. Include year picker with function: $("#yearonly").load("yearonly.html"); _x000D_
_x000D_
<div id="yearonly"></div>_x000D_
<script>_x000D_
$("#yearonly").load("yearonly.html"); _x000D_
</script>_x000D_
-->_x000D_
_x000D_
_x000D_
_x000D_
<!DOCTYPE html>_x000D_
<meta name="viewport" content="text-align:center; width=device-width, initial-scale=1.0">_x000D_
<html>_x000D_
<body>_x000D_
<style>_x000D_
.ydiv {_x000D_
border:solid 1px;_x000D_
width:200px;_x000D_
//height:150px;_x000D_
background-color:#D8D8D8;_x000D_
display:none;_x000D_
position:absolute;_x000D_
top:40px;_x000D_
}_x000D_
_x000D_
.ybutton {_x000D_
_x000D_
border: none;_x000D_
width:35px;_x000D_
height:35px;_x000D_
background-color:#D8D8D8;_x000D_
font-size:100%;_x000D_
}_x000D_
_x000D_
.yhr {_x000D_
background-color:black;_x000D_
color:black;_x000D_
height:1px">_x000D_
}_x000D_
_x000D_
.ytext {_x000D_
border:none;_x000D_
text-align:center;_x000D_
width:118px;_x000D_
font-size:100%;_x000D_
background-color:#D8D8D8;_x000D_
font-weight:bold;_x000D_
_x000D_
}_x000D_
</style>_x000D_
<p>_x000D_
<!-- input text for display result of yearpicker -->_x000D_
<input type = "text" id="yeardate"><button style="width:21px;height:21px"onclick="enabledisable()">V</button></p>_x000D_
_x000D_
<!-- yearpicker panel for change only year-->_x000D_
<div class="ydiv" id = "yearpicker">_x000D_
<button class="ybutton" style="font-weight:bold;"onclick="changedecade('back')"><</button>_x000D_
_x000D_
<input class ="ytext" id="dec" type="text" value ="2018" >_x000D_
_x000D_
<button class="ybutton" style="font-weight:bold;" onclick="changedecade('next')">></button>_x000D_
<hr></hr>_x000D_
_x000D_
_x000D_
_x000D_
<!-- subpanel with one year 0-9 -->_x000D_
<button class="ybutton" onclick="yearone = 0;setyear()">0</button>_x000D_
<button class="ybutton" onclick="yearone = 1;setyear()">1</button>_x000D_
<button class="ybutton" onclick="yearone = 2;setyear()">2</button>_x000D_
<button class="ybutton" onclick="yearone = 3;setyear()">3</button>_x000D_
<button class="ybutton" onclick="yearone = 4;setyear()">4</button><br>_x000D_
<button class="ybutton" onclick="yearone = 5;setyear()">5</button>_x000D_
<button class="ybutton" onclick="yearone = 6;setyear()">6</button>_x000D_
<button class="ybutton" onclick="yearone = 7;setyear()">7</button>_x000D_
<button class="ybutton" onclick="yearone = 8;setyear()">8</button>_x000D_
<button class="ybutton" onclick="yearone = 9;setyear()">9</button>_x000D_
</div>_x000D_
<!-- end year panel -->_x000D_
_x000D_
_x000D_
_x000D_
<script>_x000D_
var date = new Date();_x000D_
var year = date.getFullYear(); //get current year_x000D_
//document.getElementById("yeardate").value = year;// can rem if filing text from database_x000D_
_x000D_
var yearone = 0;_x000D_
_x000D_
function changedecade(val1){ //change decade for year_x000D_
_x000D_
var x = parseInt(document.getElementById("dec").value.substring(0,3)+"0");_x000D_
if (val1 == "next"){_x000D_
document.getElementById('dec').value = x + 10;_x000D_
}else{_x000D_
document.getElementById('dec').value = x - 10;_x000D_
}_x000D_
}_x000D_
_x000D_
function setyear(){ //set full year as sum decade and one year in decade_x000D_
var x = parseInt(document.getElementById("dec").value.substring(0,3)+"0");_x000D_
var y = parseFloat(yearone);_x000D_
_x000D_
var suma = x + y;_x000D_
var d = new Date();_x000D_
d.setFullYear(suma);_x000D_
var year = d.getFullYear();_x000D_
document.getElementById("dec").value = year;_x000D_
document.getElementById("yeardate").value = year;_x000D_
document.getElementById("yearpicker").style.display = "none";_x000D_
yearone = 0;_x000D_
}_x000D_
_x000D_
function enabledisable(){ //enable/disable year panel_x000D_
if (document.getElementById("yearpicker").style.display == "block"){_x000D_
document.getElementById("yearpicker").style.display = "none";}else{_x000D_
document.getElementById("yearpicker").style.display = "block";_x000D_
}_x000D_
_x000D_
}_x000D_
</script>_x000D_
</body>_x000D_
</html>
_x000D_
You can do the following:
Example using PHP (you can do this in any language of your choice):
Server:
<?php $years = range(1900, strftime("%Y", time())); ?>
HTML
<select>
<option>Select Year</option>
<?php foreach($years as $year) : ?>
<option value="<?php echo $year; ?>"><?php echo $year; ?></option>
<?php endforeach; ?>
</select>
As an added benefit, this works has a browser compatibility of a 100% ;-)
I try with this, no modifications on the css.
$(function() {_x000D_
$('#datepicker').datepicker({_x000D_
changeYear: true,_x000D_
showButtonPanel: true,_x000D_
dateFormat: 'yy',_x000D_
onClose: function(dateText, inst) {_x000D_
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();_x000D_
$(this).datepicker('setDate', new Date(year, 1));_x000D_
}_x000D_
});_x000D_
_x000D_
$("#datepicker").focus(function() {_x000D_
$(".ui-datepicker-month").hide();_x000D_
$(".ui-datepicker-calendar").hide();_x000D_
});_x000D_
_x000D_
});
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>_x000D_
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>_x000D_
<p>Date: <input type="text" id="datepicker" /></p>
_x000D_
There is input type month in HTML5 which allows to select month and year. Month selector works with autocomplete.
Check the example in JSFiddle.
<!DOCTYPE html>
<html>
<body>
<header>
<h1>Select a month below</h1>
</header>
Month example
<input type="month" />
</body>
</html>
No you can not but you may want to use input type number as a workaround. Look at the following example:
<input type="number" min="1900" max="2099" step="1" value="2016" />
Add this code structure to your page code
<?php
echo '<label>Admission Year:</label><br><select name="admission_year" data-component="date">';
for($year=1900; $year<=date('Y'); $year++){
echo '<option value="'.$year.'">'.$year.'</option>';
}
?>
It works perfectly and can be reverse engineered
<?php
echo '<label>Admission Year:</label><br><select name="admission_year" data-component="date">';
for($year=date('Y'); $year>=1900; $year++){
echo '<option value="'.$year.'">'.$year.'</option>';
}
?>
With this you are good to go.
Source: Stackoverflow.com