Example of Date Formats in ASP.NET MVC DatePicker Control
In this sample, the DatePicker has been configured with the dd-MMM-yy date format. To change this current date format, go to the properties panel at the right side and select a date format from the available options. For mobile mode touch the icon at the right side and select a date format from the available options.
@Html.UiDatePicker("datepicker", new UiDatePickerOptions
{
Placeholder = "Choose a Date",
ShowClearButton = true,
Value = (DateTime)ViewData["value"],
Format = "dd-MMM-yy",
InputFormats = new string[] { "dd/MM/yyyy", "yyyyMMdd" }
})
@Html.UiDropDownList("displayFormatDatePicker", new UiDropDownListOptions
{
Placeholder = "Select a format",
Value = "format1",
DataSource = (IEnumerable<CalendarsDemoController.DateFormats>)ViewData["data"],
Fields = new UiDropDownFields { Text = "Text", Value = "Id" },
OnChange = "changeDateFormat"
})
@Html.UiMultiSelect("inputFormatsDatePicker", new UiMultiSelectOptions
{
Placeholder = "e.g. MM/dd/yyyy",
Value = new string[] { "dd/MM/yyyy", "yyyyMMdd" },
DataSource = (string[])ViewData["formatData"],
Mode = UiMultiSelectMode.CheckBox,
ShowSelectAll = true,
OnChange = "changeInputFormats"
})
<style>
#datepicker-control { max-width: 300px; margin: 0 auto; padding-top: 50px; }
#wrapper { max-width: 350px; margin: 20px auto; }
.multi-label { margin-top: 40px; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section">
<div id="wrapper" class="datetimepicker-section">
<div id="datepicker-control">
@Html.UiDatePicker("datepicker", new UiDatePickerOptions
{
Placeholder = "Choose a Date",
ShowClearButton = true,
Value = (DateTime)ViewData["value"],
Format = "dd-MMM-yy",
InputFormats = new string[] { "dd/MM/yyyy", "yyyyMMdd" }
})
</div>
</div>
</div>
<div class="demo-property-panel" title="Properties">
<div class="demo-property-row">
<label class="example-label">Choose a display format</label>
@Html.UiDropDownList("displayFormatDatePicker", new UiDropDownListOptions
{
Placeholder = "Select a format",
Value = "format1",
DataSource = (IEnumerable<CalendarsDemoController.DateFormats>)ViewData["data"],
Fields = new UiDropDownFields { Text = "Text", Value = "Id" },
OnChange = "changeDateFormat"
})
</div>
<div class="demo-property-row">
<label class="multi-label">Choose input formats</label>
@Html.UiMultiSelect("inputFormatsDatePicker", new UiMultiSelectOptions
{
Placeholder = "e.g. MM/dd/yyyy",
Value = new string[] { "dd/MM/yyyy", "yyyyMMdd" },
DataSource = (string[])ViewData["formatData"],
Mode = UiMultiSelectMode.CheckBox,
ShowSelectAll = true,
OnChange = "changeInputFormats"
})
</div>
</div>
<script>
// Syncfusion: datepicker.format = the chosen format; datepicker.inputFormats = the chosen input formats.
function changeDateFormat(value, id) {
var item = window.Lumen.get(id).getItems().filter(function (i) { return i.value === value; })[0];
if (item) { window.Lumen.get('datepicker').setFormat(item.text); }
}
function changeInputFormats(values) {
window.Lumen.get('datepicker').setInputFormats(values);
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class DatePickerController : Controller
{
// The page is Views/DatePicker/DateFormats.cshtml; it draws what ViewData holds.
public ActionResult DateFormats()
{
ViewData["value"] = DateTime.Now;
ViewData["data"] = new DateFormats().GetDateFormatsWithId();
ViewData["formatData"] = new DateFormats().GetInputFormats();
return View();
}
public class DateFormats
{
public string Id { get; set; }
public string Text { get; set; }
public List<DateFormats> GetDateFormatsWithId()
{
return new List<DateFormats>
{
new DateFormats { Id = "format1", Text = "dd-MMM-yy" },
new DateFormats { Id = "format2", Text = "yyyy-MM-dd" },
new DateFormats { Id = "format3", Text = "dd-MMMM-yyyy" }
};
}
public string[] GetInputFormats()
{
return new string[]
{
"dd/MM/yyyy", "ddMMMyy", "yyyyMMdd", "dd.MM.yy", "MM/dd/yyyy", "yyyy/MMM/dd", "dd-MM-yyyy"
};
}
}
}
}
@* 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>
Date Formats sample illustrates the support of custom date format in the DatePicker component by using the format property. You can also change the date format by selecting it from the format options in the properties panel.
Furthermore, this example showcases the flexible date value parsing functionality available in DatePicker component. By utilizing the InputFormats property, users can enter dates in various formats, which will be automatically parsed and formatted according to the chosen date format.