Telerik blogs


We all have a variety of uses for collecting Date and Time information from a user. For example, tracking the time a patient arrives and departs from a hospital, to when a car was returned at a local car rental location to tracking expense reports. One of the controls that you may have found missing with Windows 8 XAML applications is the Date/Time Picker control. While the control exists for Windows 8 HTML Applications, it is very limited in its initial state. That is one of the reasons that Telerik has decided to make a more robust Date/Time Picker control and in this blog post we will explain how to use it in a simple app that keeps track of important appointments. So buckle up and let’s get started.

Gathering the Required Tools

I’ll make this brief, to get started follow the information posted here. After that is complete then you will need to download and install the Telerik’s Windows 8 UI Controls. Once that is in place we are ready to start working with the controls.

Let’s Begin with a StoryBoard of our App

I’m a big fan of using storyboards to visualize our applications before writing any code. For this simple one page application, I’m going to use the new PowerPoint StoryBoarding tab available if you install VS2012 Pro and up. Below you can visualize what our application looks like on the Start screen and the second screenshot shows what our single page application looks like.


Figure 1: Windows 8 Start Screen Mock


Figure 2: Appointment Tracker Mock

Please note that I prefer the light theme for storyboarding purposes.

Putting it All Together

Launch Visual Studio 2012 from the Start Screen and selecting “Templates” -> “Other Languages” -> “JavaScript” then “Windows Store”. If you are using the RC, then it located under "Windows Metro Style”. Select “Blank App” then give it a name and select the OK button as shown below.


In this sample, we are going to use the name AppointmentList. Once the project loads, we need to right click on References and select “Add Reference”.

From here, we can select “Telerik UI for JS Metro” and press OK.


If you look under References, you will see “Telerik UI for JS Metro” has been added.

Starting From Default.html

The first thing that I want to do for this sample application is setup my page to resemble the storyboard used in Figure 2. Even though we just added a reference to the Telerik controls, we are going to need to add the following Telerik JavaScript/CSS Files to our application as well. This will give us access to all of the styles/controls available to us by Telerik.

   1: <script src="///Telerik.UI/js/jquery.js" type="text/javascript"></script>
   2: <script src="///Telerik.UI/js/ui.js" type="text/javascript"></script>
   3: <link href="///Telerik.UI/css/common.css" rel="stylesheet" type="text/css">    
   4: <link href="///Telerik.UI/css/dark.css" rel="stylesheet" type="text/css">

We can also use the built-in styles supplied by Microsoft to put together a user interface quickly and easily. Copy and paste the following code and put it inside your <body> tags.

   1: <div class="fragment homepage">
   2: <header aria-label="Header content" role="banner"> 
   3: <h1 class="titlearea win-type-ellipsis">
   4: <span class="pagetitle">Appointment Tracker</span><br/>
   5: </h1>
   6: </header>
   7: <section aria-label="Main content" role="main">
   8: <p>Appointment Name:   
   9: <input id="appointmentInput" type="text">
  10: </p>
  11:      Arrival<br/> 
  12: <span data-win-control="Telerik.UI.RadDatePicker" id="arrivalDateInput"></span>
  13: <span data-win-control="Telerik.UI.RadTimePicker" id="arrivalTimeInput"></span> <br/>Departure<br/> 
  14: <span data-win-control="Telerik.UI.RadDatePicker" id="departureDateInput"></span>
  15: <span data-win-control="Telerik.UI.RadTimePicker" id="departureTimeInput"></span> 
  16: <br/><br/>
  17: <button type="button" id="saveButton" >Save</button>
  18: <button type="button">Clear All</button>
  19: <br/>
  20: </section> 
  21: <div id="appointmentOutput" ></div> 
  22: </div>

The special tags to pay attention to are:

<span data-win-control="Telerik.UI.RadDatePicker" id="arrivalDateInput"></span>
<span data-win-control="Telerik.UI.RadTimePicker" id="arrivalTimeInput"></span>

You may be surprised that is all it takes to use the control with its default settings. If we go ahead and run the application we can see the default look and feel of RadDatePicker and RadTimePicker.


One of the more commonly used scenarios of this control is formatting the way the data is displayed. Telerik has you covered there as well by the selectorFormat option. Say for example, you only wanted RadDatePicker to display the month and year. You could do that very easily by changing the selectorFormat to m/d as shown below:

<span data-win-control="Telerik.UI.RadDatePicker" data-win-options="{selectorFormat:'m/d'}"></span>

The same thing applies with the TimePicker control. Maybe you only wanted RadTimePicker to display the hour and the minutes. You could accomplish that by changing the selectorFormat to h/m as shown below:

<span data-win-control="Telerik.UI.RadTimePicker" data-win-options="{selectorFormat:'h/m'}"></span>

Adding Save Button Functionality

So far our application runs, but the Save button is not active. We are going to jump over to default.js and locate the following line:


Underneath it, add the following two lines of code:

   1: var SaveButton = document.getElementById("saveButton");
   2: SaveButton.addEventListener("click", SavebuttonClickHandler, false);

We begin by getting the id of our Save Button and adding in an event handler. Next, you need to add a function called SavebuttonClickHandler to run code once the Save button has been clicked. Add this function underneath app.oncheckpoint. Here's the function in it’s entirely:

   1: function SavebuttonClickHandler(eventInfo) {
   2: var appointmentName = document.getElementById("appointmentInput").value;
   3: var arrivalDate = document.getElementById("arrivalDateInput").textContent;
   4: var arrivalTime = document.getElementById("arrivalTimeInput").textContent;
   5: var departureDate = document.getElementById("departureDateInput").textContent;
   6: var departureTime = document.getElementById("departureTimeInput").textContent;
   8:             document.getElementById("appointmentOutput").innerText = "You have an " + appointmentName + " arriving on " + arrivalDate + " and ending on " + departureDate + " from " + arrivalTime + " until " + departureTime; }

With the addition of this function, you've defined several local variables that gets the value or textContent set in our multiple input/span tags. Finally, it outputs the values inside our appointmentOutput div by appending each local variable. Of course, in a real-world application you would probably want to do some sort of error checking to see if the values are null or not before printing to the screen. The only thing left to do now is run it and see our results:


You can see in this simple example that creating and customizing RadTimePicker/RadDatePicker is very simple and can be used in a variety of different ways. But so far, we haven’t explored Blend support or adding the controls via JavaScript. Let’s take a look at Blend Support now then we will take a look at adding the controls via JavaScript.

Download this project.

Blend for Visual Studio is Supported

We could have also created our UI in Blend for Visual Studio if we wanted to by right clicking on default.html and selecting “Open in Blend”.

If we navigate over to Assets and select “References” then “Telerik UI for JS Metro”. This will allow us to see all of the controls available in the suite.


We could simply drag and drop the controls that we wanted onto the designer and Blend would automatically add references to the Telerik CSS/JavaScript files as well as add in the proper <span> tags. We also have the ability to edit the properties more easily by looking at the HTML attributes for the controls as shown below.


Using JavaScript with RadTimePicker & RadDatePicker

The last topic that I want to talk about is adding these controls programmatically by using JavaScript. So let’s go ahead and remove everything inside the default.html page (yes, everything!) and pasting the following code inside:

   1: <!DOCTYPE html>
   2: <html>
   3: <head>
   4: <meta charset="utf-8" />
   5: <title>AppointmentList</title>
   7: <!-- WinJS references -->
   8: <link href="//Microsoft.WinJS.1.0/css/ui-dark.css" rel="stylesheet" />
   9: <script src="//Microsoft.WinJS.1.0/js/base.js"></script>
   2:     <script src="//Microsoft.WinJS.1.0/js/ui.js">
   1: </script>
   3:     <!-- AppointmentList references -->
   4:     <link href="/css/default.css" rel="stylesheet" />
   5:     <script src="/js/default.js">
   1: </script>
   2:     <script src="///Telerik.UI/js/jquery.js" type="text/javascript">
   1: </script>
   2:     <script src="///Telerik.UI/js/ui.js" type="text/javascript">
   1: </script>
   2:     <link href="///Telerik.UI/css/common.css" rel="stylesheet" type="text/css">
   3:     <link href="///Telerik.UI/css/dark.css" rel="stylesheet" type="text/css">
   4:     <script>
   5:         document.addEventListener("DOMContentLoaded", function () {
   6: var datePicker = new Telerik.UI.RadDatePicker(document.getElementById("myDatePicker"), { selectorFormat: "m/d" });
   7: var timePicker = new Telerik.UI.RadTimePicker(document.getElementById("myTimePicker"), { selectorFormat: "h:m" });
   8:         });
  11: </head>
  12: <body>
  13: <p>Appointment Date</p>
  14: <div id="myDatePicker"></div>
  15: <br>
  16: <p>Appointment Time</p> 
  17: <div id="myTimePicker"></div>
  18: </body>
  19: </html>

Heading back to our body tag, we added two divs where our span tags were. This is simply a placeholder for the Date/Time controls we are about to place inside. If you scroll up then you will see we added a small piece of JavaScript to run after the DOMContent is loaded and we declared a new instance of RadDatePicker/RadTimePicker and set the selectorFormat exactly like we did in Blend. Pretty easy stuff right?


In this blog post, we took a look at RadDatePicker and RadTimePicker part of Telerik’s Windows 8 UI Controls. We saw just how easy you can implement the controls in a real-world application using VS2012 and/or Blend for Visual Studio. I encourage you to check out the other controls available in the suite. If you would like to learn more about this control, then feel free to check out the online control documentation as well.

Thanks again and if you have any questions or comments, then feel free to drop them in the comments section below.

Win8_Download (2)

About the Author

Michael Crump

is a Microsoft MVP, Pluralsight and MSDN author as well as an international speaker. He works at Telerik with a focus on everything mobile.  You can follow him on Twitter at @mbcrump or keep up with his various blogs by visiting his Telerik Blog or his Personal Blog.


Comments are disabled in preview mode.