{"id":983,"date":"2019-11-21T11:08:01","date_gmt":"2019-11-21T11:08:01","guid":{"rendered":"https:\/\/ibex.tech\/windows-iot\/?p=983"},"modified":"2022-09-13T15:42:51","modified_gmt":"2022-09-13T14:42:51","slug":"datepicker-and-timepicker-ui-controls","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/uwp-programming-in-c\/date-and-time-uwp-programming-in-c\/datepicker-and-timepicker-ui-controls","title":{"rendered":"DatePicker and TimePicker UI Controls"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">DatePicker UI Control<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;DatePicker Canvas.Left=\"256\" Canvas.Top=\"92 \" &gt;&lt;\/DatePicker&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Popup DatePicker<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">xaml<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>    &lt;Button Canvas.Left=\"262\" Canvas.Top=\"476\" Content=\"Set Date\"&gt;\n        &lt;Button.Flyout&gt;\n            &lt;DatePickerFlyout x:Name=\"DatePicker1\" DatePicked=\"DatePicker1_DatePicked\" \/&gt;\n        &lt;\/Button.Flyout&gt;\n    &lt;\/Button&gt;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">cs<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/********************************************\n\t\/\/********************************************\n\t\/\/********** DATEPICKER DATE PICKED **********\n\t\/\/********************************************\n\t\/\/********************************************\n\tprivate async void DatePicker1_DatePicked(object sender, DatePickedEventArgs e)\n\t{\n\t\ttry\n\t\t{\n\t\t\t\/\/----- GET THE DATE ENTERED -----\n\t\t\tDateTimeOffset DateTimeOffset1 = DatePicker1.Date;\n\t\t\tint Year = DateTimeOffset1.Year;\n\t\t\tint Month = DateTimeOffset1.Month;\n\t\t\tint DayOfMonth = DateTimeOffset1.Day;\n\n\t\t\t\/\/----- SET THE SYSTEM CLOCK -----\n\t\t\tDateTime DateTime1 = new DateTime(\n\t\t\t\tYear,\n\t\t\t\tMonth,\n\t\t\t\tDayOfMonth,\n\t\t\t\tDateTime.Now.Hour,\n\t\t\t\tDateTime.Now.Minute,\n\t\t\t\tDateTime.Now.Second);\n\t\t\tDateTimeOffset DateTimeOffset2 = new DateTimeOffset(DateTime1, new TimeSpan(0, 0, 0));      \/\/The TimeSpan sets the difference from UTC which is required for DateTimeOffset\n\t\t\tWindows.System.DateTimeSettings.SetSystemDateTime(DateTimeOffset2);\t\t\t\t\t\t\t\/\/Set the system DateTime\n\n\t\t\tDateTime DateTime2 = DateTime.Now;\t\t\t\/\/Read the system DateTime\n\t\t}\n\t\tcatch (Exception ex)\n\t\t{\n\t\t\tSystem.Diagnostics.Debug.WriteLine(\"Exception: \" + ex.Message);\n\t\t}\n\t}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Popup TimePicker<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">xaml<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>    &lt;Button Canvas.Left=\"262\" Canvas.Top=\"546\" Content=\"Set Time\"&gt;\n        &lt;Button.Flyout&gt;\n            &lt;TimePickerFlyout x:Name=\"TimePicker1\" TimePicked=\"TimePicker1_TimePicked\" ClockIdentifier=\"24HourClock\" \/&gt;\n        &lt;\/Button.Flyout&gt;\n    &lt;\/Button&gt;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">cs<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/********************************************\n\t\/\/********************************************\n\t\/\/********** TIMEPICKER DATE PICKED **********\n\t\/\/********************************************\n\t\/\/********************************************\n\tprivate async void TimePicker1_TimePicked(object sender, TimePickedEventArgs e)\n\t{\n\t\ttry\n\t\t{\n\t\t\t\/\/----- GET THE TIME ENTERED -----\n\t\t\tTimeSpan TimeSpan1 = TimePicker1.Time;\n\t\t\tint Hour = TimeSpan1.Hours;\n\t\t\tint Minute = TimeSpan1.Minutes;\n\n\t\t\t\/\/----- SET THE SYSTEM CLOCK -----\n\t\t\tDateTime DateTime1 = new DateTime(\n\t\t\t\tDateTime.Now.Year,\n\t\t\t\tDateTime.Now.Month,\n\t\t\t\tDateTime.Now.Day,\n\t\t\t\tHour,\n\t\t\t\tMinute,\n\t\t\t\t0);\n\t\t\tDateTimeOffset DateTimeOffset2 = new DateTimeOffset(DateTime1, new TimeSpan(0, 0, 0));      \/\/The TimeSpan sets the difference from UTC which is required for DateTimeOffset\n\t\t\tWindows.System.DateTimeSettings.SetSystemDateTime(DateTimeOffset2);                         \/\/Set the system DateTime\n\n\t\t\tDateTime DateTime2 = DateTime.Now;          \/\/Read the system DateTime\n\t\t}\n\t\tcatch (Exception ex)\n\t\t{\n\t\t\tSystem.Diagnostics.Debug.WriteLine(\"Exception: \" + ex.Message);\n\t\t}\n\t}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Set controls to current DateTime<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/----- SETUP THE DATEPICKER AND TIEMPICKER CONTROLS -----\n\tDatePicker1.Date = DateTime.Now;\n\tTimePicker1.Time = DateTime.Now.TimeOfDay;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>DatePicker UI Control Popup DatePicker xaml cs Popup TimePicker xaml cs Set controls to current DateTime<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[138],"tags":[],"class_list":["post-983","post","type-post","status-publish","format-standard","hentry","category-date-and-time-uwp-programming-in-c"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=983"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/983\/revisions"}],"predecessor-version":[{"id":1192,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/983\/revisions\/1192"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}