如何修改Materialize日期选择器的起始年份(设置为1990年)
Hey there! To set your Materialize CSS Datepicker's starting year to 1990, you just need to add a configuration object when initializing the datepicker. Here are two straightforward approaches you can use:
Option 1: Use yearRange to Define Year Scope
This option controls the range of years displayed in the datepicker's year selector. You can set the start year as 1990 and pair it with the current year (or a fixed end year) to keep it relevant:
Updated JavaScript Code
$(document).ready(function(){ $('.datepicker').datepicker({ // Set start year to 1990, end year to current year yearRange: [1990, new Date().getFullYear()] }); });
If you want a fixed end year instead (e.g., 2025), just replace new Date().getFullYear() with your desired year:
yearRange: [1990, 2025]
Option 2: Use minDate to Restrict Earliest Selectable Date
If you want to prevent users from selecting any date before 1990 entirely, use the minDate option to set the earliest allowed date to January 1st, 1990 (note: months in JavaScript are 0-based, so January is 0):
Updated JavaScript Code
$(document).ready(function(){ $('.datepicker').datepicker({ minDate: new Date(1990, 0, 1) }); });
Your HTML Remains Unchanged
You don't need to modify your existing HTML code:
<input type="text" class="datepicker">
Pick the option that best fits your use case—either controlling the visible year range or locking down the earliest selectable date!
内容的提问来源于stack exchange,提问作者user8477089

