Home >Web Front-end >JS Tutorial >How Can I Display Only the Month and Year in a jQuery UI DatePicker?
Showing Only Month and Year in jQuery UI DatePicker
jQuery UI DatePicker is a versatile tool for displaying calendars in web applications. If you wish to use this plugin to display just the month and year, instead of the entire calendar, here's how you can do it:
A clever solution involves a slight modification to the HTML and JavaScript code. In the HTML, add a custom class to the input field where you want to display the month and year. In the JavaScript code, use this class to add the necessary options to the DatePicker configuration.
Specifically, the options you need are:
To prevent the calendar from displaying, add a CSS style to hide the calendar element (ui-datepicker-calendar).
Here's a complete example:
<label for="startDate">Date:</label> <input name="startDate">
$('.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; }
This hack will allow you to display only the month and year in the jQuery UI DatePicker without affecting its functionality.
The above is the detailed content of How Can I Display Only the Month and Year in a jQuery UI DatePicker?. For more information, please follow other related articles on the PHP Chinese website!