DateTime Range Restriction
17 Sep 20244 minutes to read
DateTime Restriction
DateTimePicker provides an option to select a date and time value within a specified range by using the min and max properties. Always the min value has to be lesser than the max value.
When the min and max properties are configured and the selected datetime value is out-of-range or invalid, then the model value will be set to out of range
datetime value or null
respectively with highlighted error
class to indicate that the datetime is out of range or invalid. The value property depends on the min/max with respect to strictMode property.
@Html.EJS().DateTimePicker("datetimepicker").Value("@ViewBag.value").Min("@ViewBag.minDate").Max("@ViewBag.maxDate").Render()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace EJ2CoreSampleBrowser.Controllers
{
public class HomeController : Controller
{
public ActionResult DateTimeRange()
{
ViewBag.minDate= new DateTime(DateTime.Now.Year,DateTime.Now.Month, 7, 0, 0, 0);
ViewBag.maxDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 27, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
ViewBag.value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 14, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
return View();
}
}
}
NOTE
If the value of
min
ormax
properties changed through code behind, then you have to update thevalue
property to set within the range.
Time Restriction
DateTimePicker provides an option to select a time value within a specified range by using the minTime and maxTime properties. Always the minTime value has to be lesser than the maxTime value.
When minTime and maxTime are set, the component will prioritize min if minTime is less than the current min time, and max if maxTime is greater than the current max time. Conversely, it will prioritize minTime if it is greater than the current min time, and maxTime if it is less than the current max time. These behaviors apply only when min and max Dates are selected or pre-bounded, with minTime and maxTime values set for all other dates apart from min and max dates.
The value property depends on the minTime/maxTime with respect to strictMode
property.
The below example allows selecting a time within the range from 10:00 AM to 8:30 PM of each day.
@Html.EJS().DateTimePicker("datetimepicker").Value("@ViewBag.value").MinTime("@ViewBag.minTime").MaxTime("@ViewBag.maxTime").Render()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace EJ2CoreSampleBrowser.Controllers
{
public class HomeController : Controller
{
public ActionResult DateTimeRange()
{
ViewBag.minTime= new DateTime(DateTime.Now.Year,DateTime.Now.Month, 7, 0, 0, 0);
ViewBag.maxTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 27, 20, 30, 0);
ViewBag.value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 14, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
return View();
}
}
}