I am using a KendoUI DateTimePicker in ASP MVC. When you select the time picker, you get hours from 00:00-24:00. This is unwieldly for people to scroll through. I only want 08:00-16:00 displayed, for any day. Is it possible to do that? Here is what I tried. This failed because it's not a valid DateTime.
@(Html.Kendo().DateTimePickerFor(x => x.HearingDate)
.Name("HearingDate")
.Min(new DateTime(0, 0, 0, 8, 0, 0))
.Max(new DateTime(0, 0, 0, 16, 0, 0))
)
EDIT: The answer I selected put me on the right path once I understood there was no way to do this with DateTimePicker. Here is my solution, blending Kendo DatePickerFor and TimePickerFor. This took many hours to figure out, mostly because of issues I had with TimePicker. In my project, The Date and Time are allowed to both be null.
Model
[Display(Name = "Hearing Date")]
public DateTime? HearingDate { get; set; }
[Display(Name = "Hearing Time")]
[DataType(DataType.Time)]
public DateTime? HearingTime { get; set; }
[Display(Name = "Hearing Date")]
public DateTime? HearingDateOnly { get; set; }
Controller
if (model.HearingDateOnly != null && model.HearingTime != null)
{
var d = model.HearingDateOnly.Value;
var t = model.HearingTime.Value;
model.HearingDate = new DateTime(d.Year, d.Month, d.Day, t.Hour, t.Minute, t.Second);
}
View
@(Html.Kendo().DatePickerFor(x => x.HearingDateOnly)
.Name("HearingDateOnly")
.Min(DateTime.Now)
)
@(Html.Kendo().TimePickerFor(x => x.HearingTime)
.Name("HearingTime")
.Min(new DateTime(2010,1,1,8, 0, 0))
.Max(new DateTime(2010,1,1, 16, 0, 0))
)
Notes: HearingDate is not shown on the view, I use it behind the scenes to join the two others. the Min and Max values are 2010 (arbitrary) datetimes, but only the time portion is used by Kendo. I had a TimeSpan, but removed it due to issues. The Display attributes are necessary to prevent Kendo's validation messages from displaying the ugly "HearingTime is not a valid date" message.
See https://stackoverflow.com/a/14501173/3250365. DateTimePicker
's time range cannot be restricted, so the workaround for this is to have a separate TimePicker
from the DatePicker
. This has other drawbacks, but then you could do:
var t = $("#time-picker").kendoTimePicker().data("kendoTimePicker");
t.min("8:00 AM");
t.max("4:00 PM");