Implementing Month and Year Only Selection with Bootstrap Datepicker

Nov 20, 2025 · Programming · 16 views · 7.8

Keywords: Bootstrap Datepicker | Month Selection | Year Selection | View Mode | Date Format

Abstract: This article provides a comprehensive guide on implementing month and year only selection functionality using Bootstrap Datepicker. It analyzes key configuration options such as viewMode, minViewMode, and startView, with detailed code examples and version compatibility considerations. The content covers date formatting, view mode control, and practical implementation techniques for developers.

Problem Background and Requirements Analysis

In real-world web development projects, date selection functionality is frequently required. Bootstrap Datepicker is a widely used jQuery plugin that provides rich date selection capabilities. However, in specific scenarios, users may only need to select months and years without requiring precise date selection. This requirement is common in business scenarios such as report statistics and data analysis.

Basic Configuration and Core Parameters

Bootstrap Datepicker provides several key configuration parameters to control date selection behavior. Among them, the format parameter defines the date display format, while viewMode and minViewMode parameters control the date picker's view mode.

In earlier versions, month and year only selection could be achieved with the following configuration:

$("#datepicker").datepicker({
    format: "mm-yyyy",
    viewMode: "months", 
    minViewMode: "months"
});

Here, format: "mm-yyyy" specifies the date format as month-year, viewMode: "months" sets the initial view to month view, and minViewMode: "months" restricts the minimum selection granularity to months, preventing users from selecting specific dates.

Version Compatibility Handling

As Bootstrap Datepicker evolved through version updates, some configuration parameters changed. Starting from version 1.2.0, the viewMode parameter was replaced by startView. Therefore, in newer versions, the following configuration should be used:

$("#datepicker").datepicker({
    format: "mm-yyyy",
    startView: "months", 
    minViewMode: "months"
});

This change reflects the evolution in plugin design, where startView more accurately describes the parameter's function — defining the initial view when the date picker opens.

HTML Structure Configuration

In addition to JavaScript configuration, proper HTML structure is crucial for functionality implementation. The basic HTML structure is as follows:

<div class="input-append date" id="datepicker" data-date="02-2012" 
     data-date-format="mm-yyyy">
    <input type="text" readonly="readonly" name="date">    
    <span class="add-on"><i class="icon-th"></i></span>      
</div>

The data-date-format attribute allows pre-defining the date format in HTML, which corresponds to the format parameter in JavaScript configuration. This design provides flexible configuration options, allowing developers to choose between HTML or JavaScript configuration based on their needs.

View Mode Detailed Explanation

Bootstrap Datepicker supports multiple view modes, each corresponding to different time granularities:

By properly combining startView and minViewMode parameters, different levels of date selection control can be achieved. For example, setting minViewMode: "months" ensures users cannot select time granularities finer than months.

Date Format Configuration

Date format strings use specific placeholders to represent different time components:

For month and year only selection scenarios, formats like mm-yyyy or MM yyyy are recommended, as they clearly display the selected time range.

Practical Application Scenarios

Month and year only selection functionality is particularly useful in the following scenarios:

  1. Financial Reports: Users need to view financial data such as income and expenses by month
  2. Sales Analysis: Analyzing sales trends and performance by month
  3. Project Planning: In project timeline planning, typically only month-level precision is required
  4. Data Statistics: Various monthly statistical data reports

Best Practice Recommendations

In actual development, following these best practices is recommended:

  1. Version Checking: Choose appropriate configuration parameters based on the Bootstrap Datepicker version being used
  2. Format Consistency: Ensure consistency between JavaScript configuration and HTML data attributes
  3. User Experience: Use the readonly attribute to prevent manual input and ensure data format uniformity
  4. Error Handling: Add appropriate error handling mechanisms to ensure fallback solutions when plugin loading fails

Extended Functionality Exploration

Beyond basic month and year selection, Bootstrap Datepicker offers other useful features:

These extended features can further enhance the date picker's practicality and user experience.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.