Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI DatePicker to show year only

Tags:

I am using jQuery datepicker to display calender.I want to know if I can use it to Display only 'Year' and not Complete Calender ??

like image 694
Nutan Avatar asked Nov 23 '12 11:11

Nutan


People also ask

How to show year only in datepicker?

Let's consider we have the below HTML code, in which we will be showing bootstrap datepicker without date part, and show only year part. $("#datepicker"). datepicker({ format: "yyyy", viewMode: "years", minViewMode: "years", autoclose:true //to close picker once year is selected });

How do you only show month and year in date picker?

Set changeMonth and changeYear to true so that Month and Year appear as Dropdown. Set date format to "MM yy". jQuery DatePicker has "onClose" event, which is called when Datepicker gets closed. So using this event, fetch the selected Month and Year and setDate of Datepicker.

How to get selected month and year from datepicker in jQuery?

datepicker({ dateFormat: 'yy-m-d', inline: true, onSelect: function(dateText, inst) { var month = dateText. getUTCMonth(); var day = dateText. getUTCDate(); var year = dateText. getUTCFullYear(); alert(day+month+year); } }); });

How can change date format in jQuery ui datepicker?

inside the jQuery script code just paste the code. $( ". selector" ). datepicker({ dateFormat: 'yy-mm-dd' });


2 Answers

**NOTE : **If anyone have objection that "why i have answered this Question now !" Because i tried all the answers of this post and got no any solution.So i tried my way and got Solution So i am Sharing to next comers****

HTML

<label for="startYear"> Start Year: </label>   <input name="startYear" id="startYear" class="date-picker-year" />    

jQuery

 <script type="text/javascript">         $(function() {             $('.date-picker-year').datepicker({                 changeYear: true,                 showButtonPanel: true,                 dateFormat: 'yy',                 onClose: function(dateText, inst) {                      var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();                     $(this).datepicker('setDate', new Date(year, 1));                 }             });         $(".date-picker-year").focus(function () {                 $(".ui-datepicker-month").hide();             });         });  </script> 
like image 129
Engr Saddam Zardari Avatar answered Oct 21 '22 22:10

Engr Saddam Zardari


  $(function() {     $('#datepicker1').datepicker( {         changeMonth: true,         changeYear: true,         showButtonPanel: true,         dateFormat: 'MM yy',         onClose: function(dateText, inst) {              var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();             var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();             $(this).datepicker('setDate', new Date(year, month, 1));         }     }); });​ 

Style should be

.ui-datepicker-calendar {     display: none;     }​ 

working demo

like image 38
Talha Avatar answered Oct 21 '22 23:10

Talha