2

我在 ASP MVC 中使用 KendoUI DateTimePicker。当您选择时间选择器时,您将获得 00:00-24:00 的小时数。这对于人们滚动浏览来说很笨拙。我只想显示 08:00-16:00,任何一天。有可能这样做吗?这是我尝试过的。这失败了,因为它不是有效的日期时间。

                    @(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))
                    )

编辑:一旦我明白使用 DateTimePicker 无法做到这一点,我选择的答案让我走上了正确的道路。这是我的解决方案,混合了 Kendo DatePickerFor 和 TimePickerFor。这花了很多时间才弄清楚,主要是因为我在使用 TimePicker 时遇到的问题。在我的项目中,日期和时间都允许为空。

模型

[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; }

控制器

        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);
        }

看法

                        @(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.

4

1 回答 1

1

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");
于 2015-07-30T00:09:50.457 回答