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;
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *