Introduction

There will be a certain scenario where, based on the user's convenience, we should provide a two-time format (12hrs/24hrs). This blog will explain how to work with Kendo Time Picker to format the time based on the user's selection of time format.

Kendo Time Picker

Kendo Time Picker is a widget to pick the time from the control, here the code to implement the Kendo Time Picker control.

KendoTimePicker.html
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <base href="https://demos.telerik.com/kendo-ui/timepicker/index">
  5. <style>html { font-size: 14px; font-family: Arial, Helvetica, sans-serif; }</style>
  6. <title></title>
  7. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.1.117/styles/kendo.common-material.min.css" />
  8. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.1.117/styles/kendo.material.min.css" />
  9. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.1.117/styles/kendo.material.mobile.min.css" />
  10. <script src="https://kendo.cdn.telerik.com/2018.1.117/js/jquery.min.js"></script>
  11. <script src="https://kendo.cdn.telerik.com/2018.1.117/js/kendo.all.min.js"></script>
  12. </head>
  13. <body>
  14. <div id="example">
  15. <div class="demo-section k-content">
  16. <h4>Set alarm time</h4>
  17. <input id="timepicker" value="12:00 AM" title="timepicker" style="width:100%;"/>
  18. </div>
  19. <script>
  20. $(document).ready(function () {
  21. $("#timepicker").kendoTimePicker({
  22. dateInput: true
  23. });
  24. });
  25. </script>
  26. </div>
  27. </body>
  28. </html>
Result

Figure 1
Now, I'm going to include the two-time format in the Kendo Time Picker using the setOptions function based on the radio button selection.
KendoTimePicker.html
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <base href="https://demos.telerik.com/kendo-ui/timepicker/index">
  5. <style>html { font-size: 14px; font-family: Arial, Helvetica, sans-serif; }</style>
  6. <title></title>
  7. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.1.117/styles/kendo.common-material.min.css" />
  8. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.1.117/styles/kendo.material.min.css" />
  9. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.1.117/styles/kendo.material.mobile.min.css" />
  10. <script src="https://kendo.cdn.telerik.com/2018.1.117/js/jquery.min.js"></script>
  11. <script src="https://kendo.cdn.telerik.com/2018.1.117/js/kendo.all.min.js"></script>
  12. </head>
  13. <body>
  14. <div id="example">
  15. <div class="demo-section k-content">
  16. <ul class="fieldlist">
  17. <li>
  18. <input type="radio" name="hrs" id="12hrs" value="12hrs" class="k-radio" checked="checked">
  19. <label class="k-radio-label" for="12hrs">12hrs</label>
  20. </li>
  21. <li>
  22. <input type="radio" name="hrs" id="24hrs" value="24hrs" class="k-radio" >
  23. <label class="k-radio-label" for="24hrs">24hrs</label>
  24. </li>
  25. </ul>
  26. <h4>Set alarm time</h4>
  27. <input id="timepicker" value="12:00 AM" title="timepicker" style="width:50%;"/>
  28. </div>
  29. <script>
  30. $(document).ready(function () {
  31. $("#timepicker").kendoTimePicker({
  32. dateInput: true
  33. });
  34. });
  35. $("input[name='hrs']").click(function(){
  36. var timepicker = $("#timepicker").data("kendoTimePicker");
  37. if($('input:radio[name=hrs]:checked').val() == "24hrs"){
  38. timepicker.setOptions({
  39. format: 'HH: mm '
  40. })
  41. }
  42. else
  43. {
  44. timepicker.setOptions({
  45. format: 'hh:mm tt'
  46. })
  47. }
  48. });
  49. </script>
  50. <style>
  51. .fieldlist {
  52. margin: 0 0 -1em;
  53. padding: 0;
  54. }
  55. .fieldlist li {
  56. list-style: none;
  57. padding-bottom: 1em;
  58. }
  59. </style>
  60. </div>
  61. </body>
  62. </html>
From the above code, it is clear that based on radio button selection the time format of the Time Picker will get changed.
Figure 2
I hope you have learned from this blog. Your valuable feedback, questions, or comments about this blog are always welcome.
G
M
T
Text-to-speech function is limited to 200 characters
Options : History : Feedback : DonateClose