DatePicker.MinYear Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the minimum Gregorian year available for picking.
public:
property DateTime MinYear { DateTime get(); void set(DateTime value); };
DateTime MinYear();
void MinYear(DateTime value);
public System.DateTimeOffset MinYear { get; set; }
var dateTime = datePicker.minYear;
datePicker.minYear = dateTime;
Public Property MinYear As DateTimeOffset
Property Value
The minimum Gregorian year available for picking.
Examples
This example demonstrates setting the MinYear
and MaxYear
properties in code.
<DatePicker x:Name="myDatePicker"/>
public MainPage()
{
this.InitializeComponent();
myDatePicker.MinYear = new DateTimeOffset(new DateTime(1950, 1, 1));
myDatePicker.MaxYear = DateTimeOffset.Now.AddYears(5);
}
Remarks
You can set the MinYear
and MaxYear properties to restrict the date values in the picker. By default, MinYear
is set to 100 years prior to the current date and MaxYear
is set to 100 years past the current date.
If you set only MinYear
or MaxYear
, you need to ensure that a valid date range is created by the date you set and the default value of the other date; otherwise, no date will be available to select in the picker. For example, setting only yearDatePicker.MaxYear = new DateTimeOffset(new DateTime(900, 1, 1));
creates an invalid date range with the default value of MinYear
.
The MinYear property can't be set as a XAML attribute string, because the Windows Runtime XAML parser doesn't have a conversion logic for converting strings to dates as DateTime / DateTimeOffset objects. Here are some suggested ways these objects can be defined in code and set to a date other than the current date.
- DateTime: Instantiate a Windows.Globalization.Calendar object (it is initialized to the current date). Set Year, or call AddYears, to adjust the date. Then, call Calendar.GetDateTime and use the returned DateTime to set MinYear.
- DateTimeOffset: Call the constructor. For the inner System.DateTime, use the constructor signature. Or, construct a default DateTimeOffset (it is initialized to the current date) and call AddYears.
Another possible technique is to define a date that's available as a data object or in the data context, then set MinYear as a XAML attribute that references a {Binding} markup extension that can access the date as data.