Search results

DateTime Range in ASP.NET MVC DateTimePicker control

20 Jan 2022 / 1 minute to read

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.

Copied to clipboard
Copied to clipboard
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();

 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.