Home >Web Front-end >JS Tutorial >How Can I Display Only the Month and Year in a jQuery UI DatePicker?

How Can I Display Only the Month and Year in a jQuery UI DatePicker?

DDD
DDDOriginal
2024-11-23 21:32:12421browse

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:

  • changeMonth: Allow changing the month
  • changeYear: Allow changing the year
  • showButtonPanel: Show the button panel with "Done" and "Today" buttons
  • dateFormat: Specify the output format as 'MM yy' (e.g., May 10)
  • onClose: A callback function to update the input field with the selected month and year

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn