Example of Format in ASP.NET MVC DateRangePicker Control
In this sample, the DateRangePicker has been configured with the dd/MMM/yy hh:mm a date time format. To change this current date time format, go to the properties panel at the right side and select a date format from the availbale options. For mobile mode touch the icon at the right side and select a date time format from the availbale options.
@Html.UiDateRangePicker("Rangepicker_start", "Rangepicker_end", new UiDateRangePickerOptions
{
Id = "Rangepicker",
Format = "dd/MMM/yy hh:mm a",
StartDate = (DateTime)ViewData["start"],
EndDate = (DateTime)ViewData["end"],
InputFormats = new string[] { "dd/MM/yyyy", "yyyyMMdd" }
})
@Html.UiDropDownList("displayFormatDatePicker", new UiDropDownListOptions
{
Placeholder = "Select a format",
Value = "format1",
DataSource = (IEnumerable<CalendarsDemoController.DateRangeFormats>)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>
#dateRangepicker-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="dateRangepicker-section">
<div id="dateRangepicker-control">
@Html.UiDateRangePicker("Rangepicker_start", "Rangepicker_end", new UiDateRangePickerOptions
{
Id = "Rangepicker",
Format = "dd/MMM/yy hh:mm a",
StartDate = (DateTime)ViewData["start"],
EndDate = (DateTime)ViewData["end"],
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.DateRangeFormats>)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: the separator is "to" for the 24-hour year-first format, "-" for the others; then the format is set.
function changeDateFormat(value, id) {
var item = window.Lumen.get(id).getItems().filter(function (i) { return i.value === value; })[0];
if (!item) { return; }
var picker = window.Lumen.get('Rangepicker');
picker.setSeparator(item.text === 'yyyy/MM/dd HH:mm' ? ' to ' : ' - ');
picker.setFormat(item.text);
}
function changeInputFormats(values) {
window.Lumen.get('Rangepicker').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 DateRangePickerController : Controller
{
// The page is Views/DateRangePicker/Format.cshtml; it draws what ViewData holds.
public ActionResult Format()
{
var startDate = DateTime.Now;
var endDate = DateTime.Now.AddDays(20);
ViewData["start"] = startDate;
ViewData["end"] = endDate;
ViewData["data"] = new DateRangeFormats().GetDateFormatsWithId();
ViewData["formatData"] = new DateRangeFormats().GetInputFormats();
return View();
}
public class DateRangeFormats
{
public string Id { get; set; }
public string Text { get; set; }
public List<DateRangeFormats> GetDateFormatsWithId()
{
return new List<DateRangeFormats>
{
new DateRangeFormats { Id = "format1", Text = "dd/MMM/yy hh:mm a" },
new DateRangeFormats { Id = "format2", Text = "yyyy-MM-dd" },
new DateRangeFormats { Id = "format3", Text = "dd-MMMM-yyyy" },
new DateRangeFormats { Id = "format4", Text = "yyyy/MM/dd HH:mm" }
};
}
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>
Format sample illustrates the support of custom date time format in the DateRangePicker component by using the format property. You can also change the date time format by selecting it from the format options in the properties panel.
Furthermore, this example showcases the flexible date value parsing functionality available in DateRangePicker 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.