Example of Special Dates in ASP.NET MVC DateTimePicker Control
In the following sample, specific dates are highlighted. In desktop mode highlighted information about the date will be displayed when hovered.
@Html.UiDateTimePicker("datetimepicker", new UiDateTimePickerOptions { OnRenderDayCell = "customDates", Value = (DateTime)ViewData["value"] })
<style>
#wrapper { max-width: 246px; margin: 30px auto; padding-top: 50px; }
.flatpickr-day.special { position: relative; }
.flatpickr-day.special span.highlight { position: absolute; left: 50%; bottom: 3px; width: 4px; height: 4px; margin-left: -2px; border-radius: 50%; background: rgb(0, 0, 255); }
</style>
<div class="control-section">
<div id="wrapper">
@Html.UiDateTimePicker("datetimepicker", new UiDateTimePickerOptions { OnRenderDayCell = "customDates", Value = (DateTime)ViewData["value"] })
</div>
</div>
<script>
// Syncfusion: the 10th, the 15th and the 25th are special days with a title.
function customDates(args) {
if (args.date.getDate() === 10) { specialDate(args, 'Birthday'); }
if (args.date.getDate() === 15) { specialDate(args, 'Farewell'); }
if (args.date.getDate() === 25) { specialDate(args, 'Vacation'); }
}
function specialDate(args, name) {
var mark = document.createElement('span');
mark.className = 'highlight';
args.element.classList.add('special', name.toLowerCase());
args.element.title = name + '!';
args.element.appendChild(mark);
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class DateTimePickerController : Controller
{
// The page is Views/DateTimePicker/SpecialDates.cshtml; it draws what ViewData holds.
public ActionResult SpecialDates()
{
ViewData["value"] = DateTime.Now;
return View();
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
Special Dates sample demonstrates how to customize specific dates in the DateTimePicker by using renderDayCell event. This event gets triggered on each day cell element creation, that allows you to customize, or disable specific dates in the DateTimePicker. Here the Federal holidays in USA for the year of 2017 are highlighted with custom styles and classes.