DateTime Range

19 Dec 20222 minutes to read

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.

<ejs-datetimepicker id="datetimepicker" value="@ViewBag.value" min="@ViewBag.minDate" max="@ViewBag.maxDate"></ejs-datetimepicker>
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 or max properties changed through code behind, then you have to update the value property to set within the range.