{"id":619,"date":"2015-03-12T15:56:27","date_gmt":"2015-03-12T22:56:27","guid":{"rendered":"http:\/\/appinventor.pevest.com\/?p=619"},"modified":"2015-03-12T15:56:27","modified_gmt":"2015-03-12T22:56:27","slug":"using-timepicker-and-datepicker-for-entering-time-and-date-information","status":"publish","type":"post","link":"https:\/\/coldstreams.com\/appinventor\/2015\/03\/12\/using-timepicker-and-datepicker-for-entering-time-and-date-information\/","title":{"rendered":"Using TimePicker and DatePicker for entering time and date information"},"content":{"rendered":"<p><strong>The TimePicker and DatePicker User Interface Controls<\/strong><\/p>\n<p>Entering the date and time are common features of business applications. We could use a text edit box and let the user type in times (like 10:30) or dates (12 January 2015) but both methods require the user to enter the time or date in the proper format &#8211; and the app needs to test the entered data to ensure it was entered correctly.<\/p>\n<p>A better solution is to use App Inventors <em>TimePicker<\/em> and <em>DatePicker<\/em> controls. Both provide a graphical method of selecting input values. For example, the <em>TimePicker<\/em> displays the following:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/03\/TimeDate_TimePickerUI.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-623\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/03\/TimeDate_TimePickerUI-576x1024.png\" alt=\"TimeDate_TimePickerUI\" width=\"360\" height=\"640\" \/><\/a>The time is set by pressing the + or &#8211; buttons above and below the hours and minutes. The AM\/PM indicator is a toggle &#8211; when it shows PM, a press changes it to AM, and when it shows AM, a press changes it to PM. With this input system, the user can never entered an invalid time (the user could, of course, enter the wrong time, but that is a different problem!)<\/p>\n<p><!--more--><\/p>\n<p><em>DatePicker<\/em> presents a similar display but instead of hours and minutes, <em>DatePicker<\/em> shows month, day of month, and the year. (I suspect this control follows the date format specified for the device &#8211; some countries write dates as month\/day\/year and some write dates as day\/month\/year. I did not check to see if this is the case but am guessing the control follows the country format specified for the device.)<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/03\/TimeDate_DatePickerUI.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-622\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/03\/TimeDate_DatePickerUI-576x1024.png\" alt=\"TimeDate_DatePickerUI\" width=\"360\" height=\"640\" \/><\/a><\/p>\n<p><strong>The Designer View<\/strong><\/p>\n<p>To demonstrate use of <em>TimePicker<\/em> and <em>DatePicker<\/em>, I created this simple program with a button press to select a date and another to select the time. After selecting either the date or time, the value selected is displayed on screen:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-621\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/03\/TimeDateUI-AfterTimePick-576x1024.png\" alt=\"TimeDateUI-AfterTimePick\" width=\"278\" height=\"494\" \/><\/p>\n<p>This screen layout (see below) is defined using a vertical layout inside of a horizontal layout, at top, and then a two horizontal layouts inside a vertical layout where the values are displayed on the screen.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/03\/TimeDateDesigner.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-626\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/03\/TimeDateDesigner.png\" alt=\"TimeDateDesigner\" width=\"360\" height=\"584\" \/><\/a><\/p>\n<p>The item <em>Select Date<\/em> looks like a button control &#8211; but it is actually a <em>DatePicker<\/em> control. Select, click and drag the <em>DatePicker<\/em> control from the User Interface Palette in the App Inventor Designer, and then drop it on to the app design canvas. Do the same for\u00a0<em>TimePicker<\/em>.<\/p>\n<p>Then, select the control on the canvas (click with your mouse) and go over to the Text field in the Properties area and change the text to read &#8220;Select Date&#8221; or &#8220;Select Time&#8221; depending on the control.<\/p>\n<p>You also need a <em>Clock<\/em> control, found in the Sensors section of the Palette. Drag the <em>Clock<\/em> control to the canvas. The <em>Clock<\/em> is a non-visible control that appears beneath the app user interface layout.<\/p>\n<p><strong>Blocks Code<\/strong><\/p>\n<p>To obtain\u00a0the current date and time, our app calls the <em>Clock1<\/em> control. \u00a0Times are stored in an internal format that is based on milliseconds, since the year 1970. This peculiar format is produced by the Clock control but must be converted into year, month, day, and hour and minute values.<\/p>\n<p>Step 1 is to obtain the <em>CurrentTime<\/em>. This is done by referencing the <em>Clock1.SystemTime<\/em> property (which is in the millisecond format) and then passing that to the <em>Clock1.MakeInstantFromMills<\/em> method and saving the result in the variable <em>CurrentTime<\/em>.<\/p>\n<p>Step 2 converts the <em>CurrentTime<\/em> value by\u00a0using additional <em>Clock1<\/em> methods. for the <em>Year<\/em>, <em>Month<\/em> and <em>DayofMonth<\/em>.<\/p>\n<p>Step 3 obtains the hours and minutes but does so using an alternative method of calling <em>Clock1.Now<\/em> to obtain the current time. (We could have used this in place of the <em>MakeInstantFromMill<\/em> and <em>SystemTime<\/em> method shown above.)<\/p>\n<p>Both methods are shown in these example blocks:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/03\/TimeDateInitialize.png\"><img loading=\"lazy\" decoding=\"async\" class=\"  wp-image-627 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/03\/TimeDateInitialize-1024x657.png\" alt=\"TimeDateInitialize\" width=\"580\" height=\"372\" \/><\/a><\/p>\n<p>The <em>DatePicker<\/em> is automatically activated when the<em> Set Date<\/em> button is pressed; it is not necessary to call the <em>DatePicker<\/em> or to implement an event handler. Usage is automatic. However, once the date has been selected, an\u00a0.<em>AfterDateSet<\/em> event occurs and this is where code needs to be inserted to make use of the selected date. \u00a0In this example, the selected year, month and day are converted into a text string and displayed on the app screen:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/03\/TimeDate_DatePicker.png\"><img loading=\"lazy\" decoding=\"async\" class=\"  wp-image-624 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/03\/TimeDate_DatePicker.png\" alt=\"TimeDate_DatePicker\" width=\"548\" height=\"178\" \/><\/a><\/p>\n<p>The <em>TimePicker<\/em> is similar to <em>DatePicker<\/em> &#8211; <em>TimePicker<\/em> is activated automatically when the Set Time button is pressed on screen. Once the time has been selected, code processes the <em>AfterTimeSet<\/em> event and obtain the <em>TimePicker1.Hour<\/em> and <em>TimePicker1.Minute<\/em> values.<\/p>\n<p>We could stop here, if we wished. However, the time is returned in 24 hour time.\u00a0 Extra code has been added to reformat the time into the familiar 12-hour HH:MM AM or PM format. The <em>if-then, else-if-then, else-if-then<\/em> compound blocks perform the conversion of the 24 hour time to 12 hour time, and set the AM or PM indicator. You may notice we never set the AM indication &#8211; that&#8217;s because the indicator is initially set to AM. Only if the time is greater than 12 noon is the indicator changed\u00a0to PM.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/03\/TimeDate_TimePicker.png\"><img loading=\"lazy\" decoding=\"async\" class=\"  wp-image-625 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/03\/TimeDate_TimePicker-801x1024.png\" alt=\"TimeDate_TimePicker\" width=\"524\" height=\"670\" \/><\/a><\/p>\n<p>With this code, you have the pieces for entering dates and times. In a business oriented application, these values might be stored in a TinyDB or a Google Fusion table.<\/p>\n<p><strong>Key Features Shown<\/strong><\/p>\n<ul>\n<li>Use of the TimePicker and DatePicker in the user interface design<\/li>\n<li>Processing the return values from both TimePicker and DatePicker<\/li>\n<li>How to convert the 24-hour time into 12-hour AM\/PM format.<\/li>\n<\/ul>\n<p><strong>Downloads<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/appinventor.pevest.com\/source\/tutorials\/TimeDate.aia\">TimeDate.aia<\/a> App Inventor\u00a0source file<\/li>\n<li>Download the source code to your computer. Then, in App Inventor, go to the Projects menu and select \u201cImport project (.aia) from my computer\u2026\u201d<\/li>\n<\/ul>\n<p><strong>Please Share on Social Media<\/strong><\/p>\n<p>Please click on the buttons below this post to share with your friends on Facebook or other social media.<\/p>\n<p>If you are not already following this blog, click on the following links to like on Facebook, add to your Google+ circles or follow on Twitter or in your RSS news reader. Thank you for visiting!<\/p>\n<p><a href=\"https:\/\/www.facebook.com\/appinventor2\"><img decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/facebook-like.png\" alt=\"\" width=\"150\" \/><\/a><a href=\"https:\/\/plus.google.com\/107302082825289724871\/posts\"><img decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/coldstreams.com\/images\/plus-badge.png\" alt=\"\" width=\"48\" \/><\/a><a href=\"http:\/\/twitter.com\/appinventorplus\"><img decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/coldstreams.com\/images\/twitter-follow.png\" alt=\"\" width=\"100\" \/><\/a><a href=\"http:\/\/appinventor.pevest.com\/?feed=rss2\"><img decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/coldstreams.com\/images\/rss.png\" alt=\"\" \/><\/a><\/p>\n<p><em><strong>Afterword<\/strong><\/em><\/p>\n<p>A couple of weeks back, I had an ear problem that left me dizzy for a few days. That cleared up, fortunately. Then I was on a short vacation, got back, and promptly got sick with a terrific cold. Only just now climbing back from that. Anyway, that&#8217;s why posting here has been sparse, plus with my time intensive volunteer robotics work at a local high school going on until 2 weeks ago too.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The TimePicker and DatePicker User Interface Controls Entering the date and time are common features of business applications. We could use a text edit box and let the user type in times (like 10:30) or dates (12 January 2015) but both methods require the user to enter the time or date in the proper format &hellip; <a href=\"https:\/\/coldstreams.com\/appinventor\/2015\/03\/12\/using-timepicker-and-datepicker-for-entering-time-and-date-information\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using TimePicker and DatePicker for entering time and date information<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[19,22,28,55,56,70,71,177,197,198],"class_list":["post-619","post","type-post","status-publish","format-standard","hentry","category-components","tag-android","tag-app-inventor","tag-appinventor","tag-clock","tag-clock1","tag-date","tag-datepicker","tag-software-development","tag-time","tag-timepicker"],"_links":{"self":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/comments?post=619"}],"version-history":[{"count":0,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/619\/revisions"}],"wp:attachment":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/media?parent=619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/categories?post=619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/tags?post=619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}