DatePicker Control For Windows 10

DatePicker represents a control that allows a user to pick a date value. 

Step 1: Open a blank app and add DatePicker and Textblock control either from the toolbox or by copying the following XAML code into your grid. 

  1. <TextBlock Text=" Time Picker " FontSize="20"></TextBlock>  
  2. <StackPanel Margin="20,30,0,0">  
  3.     <TimePicker Name="myTime" Header="Select a time" ClockIdentifier="12HourClock" TimeChanged="myTime_TimeChanged" Margin="15,0,0,0"></TimePicker>  
  4.     <TextBlock x:Name="selectedTime" Margin="15,15,0,0" Foreground="Green" /> </StackPanel>   

The Header defines a small text above the DatePicker control.

 

Step 2: If you want to show the day of the week along with date you can add the following code to the DatePicker XAML code.

  1. DayFormat="{}{day.integer} {dayofweek.full}"  

 

Step 3: Copy and paste the following code to the myDate_DateChanged event in the cs page.

  1. DateTimeFormatter dtf = new DateTimeFormatter("shortdate");  
  2. selectedDate.Text = "The selected date is " + dtf.Format(e.NewDate).ToString();  
Step 4: Run your application and select a date. It will be shown in our TextBlock assigned. 

 

Step 5:
 If you want to assign a Minimum and a Maximum year for the calendar you can also add the following code to the class.
  1. this.myDate.MinYear = new DateTimeOffset(new DateTime(1990, 1, 1));  
  2. this.myDate.MaxYear = new DateTimeOffset(new DateTime(2016, 12, 1));