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
Release: 2024-11-23 21:32:12
Original
468 people have browsed it

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">
Copy after login
$('.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));
  }
});
Copy after login
.ui-datepicker-calendar {
  display: none;
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template