DatePicker UI Control
<DatePicker Canvas.Left="256" Canvas.Top="92 " ></DatePicker>
Popup DatePicker
xaml
<Button Canvas.Left="262" Canvas.Top="476" Content="Set Date">
<Button.Flyout>
<DatePickerFlyout x:Name="DatePicker1" DatePicked="DatePicker1_DatePicked" />
</Button.Flyout>
</Button>
cs
//********************************************
//********************************************
//********** DATEPICKER DATE PICKED **********
//********************************************
//********************************************
private async void DatePicker1_DatePicked(object sender, DatePickedEventArgs e)
{
try
{
//----- GET THE DATE ENTERED -----
DateTimeOffset DateTimeOffset1 = DatePicker1.Date;
int Year = DateTimeOffset1.Year;
int Month = DateTimeOffset1.Month;
int DayOfMonth = DateTimeOffset1.Day;
//----- SET THE SYSTEM CLOCK -----
DateTime DateTime1 = new DateTime(
Year,
Month,
DayOfMonth,
DateTime.Now.Hour,
DateTime.Now.Minute,
DateTime.Now.Second);
DateTimeOffset DateTimeOffset2 = new DateTimeOffset(DateTime1, new TimeSpan(0, 0, 0)); //The TimeSpan sets the difference from UTC which is required for DateTimeOffset
Windows.System.DateTimeSettings.SetSystemDateTime(DateTimeOffset2); //Set the system DateTime
DateTime DateTime2 = DateTime.Now; //Read the system DateTime
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
}
}
Popup TimePicker
xaml
<Button Canvas.Left="262" Canvas.Top="546" Content="Set Time">
<Button.Flyout>
<TimePickerFlyout x:Name="TimePicker1" TimePicked="TimePicker1_TimePicked" ClockIdentifier="24HourClock" />
</Button.Flyout>
</Button>
cs
//********************************************
//********************************************
//********** TIMEPICKER DATE PICKED **********
//********************************************
//********************************************
private async void TimePicker1_TimePicked(object sender, TimePickedEventArgs e)
{
try
{
//----- GET THE TIME ENTERED -----
TimeSpan TimeSpan1 = TimePicker1.Time;
int Hour = TimeSpan1.Hours;
int Minute = TimeSpan1.Minutes;
//----- SET THE SYSTEM CLOCK -----
DateTime DateTime1 = new DateTime(
DateTime.Now.Year,
DateTime.Now.Month,
DateTime.Now.Day,
Hour,
Minute,
0);
DateTimeOffset DateTimeOffset2 = new DateTimeOffset(DateTime1, new TimeSpan(0, 0, 0)); //The TimeSpan sets the difference from UTC which is required for DateTimeOffset
Windows.System.DateTimeSettings.SetSystemDateTime(DateTimeOffset2); //Set the system DateTime
DateTime DateTime2 = DateTime.Now; //Read the system DateTime
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
}
}
Set controls to current DateTime
//----- SETUP THE DATEPICKER AND TIEMPICKER CONTROLS -----
DatePicker1.Date = DateTime.Now;
TimePicker1.Time = DateTime.Now.TimeOfDay;
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.