0

$(".datepicker").datepicker({
  changeMonth: true,
  changeYear: true,
  dateFormat: 'yy-mm-dd',
  yearRange: '1800:2100',
  onSelect: function(value, ui) {


  }
});
$('.yearmonth').datepicker({
  changeMonth: true,
  changeYear: true,
  showButtonPanel: true,
  dateFormat: 'yy-mm',
  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));
  }

});
.yearmonth .ui-datepicker-calendar {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
Year and Month : <input class="yearmonth" type="text"  /><br>
Complete Date : <input class="datepicker" type="text"  />

How can I have 2 datepicker in one context that will have a full date and the other will have only month and year?

Tried hiding using CSS but I cant determine how select the datepicker that is only for month and year

What I want is when I click on year month the calendar should be hidden.

Giant
  • 1,619
  • 7
  • 33
  • 67

3 Answers3

1

Use below code. Change the dateFormat: 'MM yy' according to your requirement.

$(function() {
    $('.date-picker').datepicker( {
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'MM yy',
        onClose: function(dateText, inst) { 
            $(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
        }
    });
});
.ui-datepicker-calendar {
    display: none;
    }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
    <link rel="stylesheet" type="text/css" media="screen" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css">
<body>
    <label for="startDate">Date :</label>
    <input name="startDate" id="startDate" class="date-picker" />
</body>
RJParikh
  • 4,096
  • 1
  • 19
  • 36
0

Change the format dateFormat: 'yy-mm', or 'yy-mm-dd',

$(".datepicker").datepicker({
  changeMonth: true,
  changeYear: true,
  dateFormat: 'yy-mm',
  yearRange: '1800:2100',
  onSelect: function(value, ui) {


  }
});
$('.yearmonth').datepicker({
  changeMonth: true,
  changeYear: true,
  showButtonPanel: true,
  dateFormat: 'yy-mm',
  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));
  }

});
.yearmonth .ui-datepicker-calendar {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
Year and Month : <input class="yearmonth" type="text"  /><br>
Complete Date : <input class="datepicker" type="text"  />
0

To hide calendar after clicking on month or year you need to add onChangeMonthYear event function in datepicker.

Try this

$("#datep").datepicker({
    changeMonth: true,
    changeYear: true,
   onChangeMonthYear: function(year, month, widget) {
        setTimeout(function() {
           $('.ui-datepicker-calendar').hide();
        });
    },
});
NIKHIL PARIHAR
  • 502
  • 6
  • 10