|
| 1 | +--- |
| 2 | +title: Time duration input for more than 24 hours |
| 3 | +description: How to create a time duration input component that accepts more than 24 hours? |
| 4 | +type: how-to |
| 5 | +page_title: Time duration input for more than 24 hours |
| 6 | +slug: common-kb-time-duration-input |
| 7 | +position: |
| 8 | +tags: date, time, input, more than 24 |
| 9 | +ticketid: 1540856 |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>DateTimePicker for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +I want to have an input which I can use to set a time duration of more than 24 hours. I tried DateTimePicker but could not exceed 24 hours. |
| 27 | + |
| 28 | +The use case would be to choose a start datetime and then the user inputs a time duration based on which the end datetime will be calculated. How can I achieve this? |
| 29 | + |
| 30 | +## Solution |
| 31 | + |
| 32 | +You can create a custom input for time duration using a [MaskedTextBox]({%slug maskedtextbox-overview%}). The used mask could be `00:00:00` and you can [include the literals in the value]({%slug maskedtextbox-mask-prompt%}#include-literals-in-the-value), so you can then split the MaskedTextBox value string by them and assign the separate hours, minutes, seconds values to corresponding variables. |
| 33 | + |
| 34 | +Then, use the `AddHours`, `AddMinutes`, `AddSeconds` methods of `DateTime` objects to add the hours, minutes, seconds values to the start datetime and thus calculate the end datetime. |
| 35 | + |
| 36 | +For a better UX, you can also add some custom validation to check whether the minutes and seconds value exceed 60. |
| 37 | + |
| 38 | +The sample below demonstrates the described approach. |
| 39 | + |
| 40 | +````CSHTML |
| 41 | +<style> |
| 42 | + .k-state-invalid .k-textbox { |
| 43 | + border-color: rgba(213,25,35,.5); |
| 44 | + } |
| 45 | +</style> |
| 46 | +
|
| 47 | +@if (Invalid) |
| 48 | +{ |
| 49 | + <div class="k-validation-summary k-messagebox k-messagebox-error" role="alert"> |
| 50 | + <ul> |
| 51 | + <li> |
| 52 | + <a style="text-decoration: none">@ErrorMessage</a> |
| 53 | + </li> |
| 54 | + </ul> |
| 55 | + </div> |
| 56 | +} |
| 57 | +
|
| 58 | +<div class="k-form"> |
| 59 | + <div class="k-form-field"> |
| 60 | + <label class="k-label k-form-label">Start Time:</label> |
| 61 | + <TelerikDateTimePicker @bind-Value="@StartTime" |
| 62 | + Format="dd MMM yyyy HH:mm:ss" |
| 63 | + Width="250px"> |
| 64 | + </TelerikDateTimePicker> |
| 65 | + </div> |
| 66 | + <div class="k-form-field"> |
| 67 | + <TelerikMaskedTextBox Mask="00:00:00" |
| 68 | + Value="@DurationValue" |
| 69 | + ValueChanged="@ValueChangedHandler" |
| 70 | + IncludeLiterals="true" |
| 71 | + Class="@(Invalid? "k-state-invalid" : "")" |
| 72 | + Width="250px" |
| 73 | + Label="Duration:"> |
| 74 | + </TelerikMaskedTextBox> |
| 75 | + </div> |
| 76 | +
|
| 77 | + <div class="k-form-field"> |
| 78 | + <TelerikButton OnClick="CalculateEnd" Enabled="@(!Invalid)">Calculate End</TelerikButton> |
| 79 | + </div> |
| 80 | +
|
| 81 | + <div class="k-form-field"> |
| 82 | + <label class="k-label k-form-label">End Time:</label> |
| 83 | + <TelerikDateTimePicker @bind-Value="@EndTime" |
| 84 | + Format="dd MMM yyyy HH:mm:ss" |
| 85 | + Width="250px"> |
| 86 | + </TelerikDateTimePicker> |
| 87 | + </div> |
| 88 | +</div> |
| 89 | +
|
| 90 | +@code { |
| 91 | + string DurationValue { get; set; } |
| 92 | +
|
| 93 | + public DateTime StartTime { get; set; } = DateTime.Now; |
| 94 | + public DateTime? EndTime { get; set; } |
| 95 | +
|
| 96 | + public double Hours { get; set; } |
| 97 | + public double Minutes { get; set; } |
| 98 | + public double Seconds { get; set; } |
| 99 | +
|
| 100 | + string ErrorMessage; |
| 101 | + bool Invalid; |
| 102 | +
|
| 103 | + void ValueChangedHandler(string newVal) |
| 104 | + { |
| 105 | + DurationValue = newVal; |
| 106 | +
|
| 107 | + if (DurationValue != null) |
| 108 | + { |
| 109 | + string[] duration = DurationValue.Split(":"); |
| 110 | +
|
| 111 | + Hours = string.IsNullOrWhiteSpace(duration.ElementAtOrDefault(0)) ? 0 : double.Parse(duration[0]); |
| 112 | + Minutes = string.IsNullOrWhiteSpace(duration.ElementAtOrDefault(1)) ? 0 : double.Parse(duration[1]); |
| 113 | + Seconds = string.IsNullOrWhiteSpace(duration.ElementAtOrDefault(2)) ? 0 : double.Parse(duration[2]); |
| 114 | +
|
| 115 | + Validate(); |
| 116 | + } |
| 117 | + } |
| 118 | +
|
| 119 | + void Validate() |
| 120 | + { |
| 121 | + if (Minutes > 60) |
| 122 | + { |
| 123 | + Invalid = true; |
| 124 | + ErrorMessage = "Minutes cannot be more than 60"; |
| 125 | + } |
| 126 | + else if (Seconds > 60) |
| 127 | + { |
| 128 | + Invalid = true; |
| 129 | + ErrorMessage = "Seconds cannot be more than 60"; |
| 130 | + } |
| 131 | + else |
| 132 | + { |
| 133 | + Invalid = false; |
| 134 | + } |
| 135 | + } |
| 136 | +
|
| 137 | + void CalculateEnd() |
| 138 | + { |
| 139 | + if (!Invalid) |
| 140 | + { |
| 141 | + EndTime = StartTime.AddHours(Hours).AddMinutes(Minutes).AddSeconds(Seconds); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | +```` |
0 commit comments