Search results

DateTime Range

DateTimePicker provides an option to select a date and time value within a specified range by using the min and maxproperties. 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 indicates the datetime is out of range or invalid

The value property depends on the min/max with respect to strictMode property.

The below example allows selecting a date within the range from 7th to 27th day in a month.

razor
datetimerange.cs
@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;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace EJ2CoreSampleBrowser.Controllers
{
    public partial class HomeController : Controller
    {
        // GET: /<controller>/
        public IActionResult 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();
        }
    }
}

 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.