Example of Format in ASP.NET MVC DateTimePicker Control
In this sample, the DateTimePicker 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 available options. For mobile mode touch the icon at the right side and select a date time format from the available options.
@Html.UiDateTimePicker("datetimepicker", new UiDateTimePickerOptions
{
Placeholder = "Choose a Date and Time",
ShowClearButton = true,
Value = (DateTime)ViewData["value"],
Format = "dd-MMM-yy hh:mm a",
InputFormats = new string[] { "MM.dd.yyyy hh:mm a", "yyyy-MM-dd HH:mm" }
})
@Html.UiDropDownList("displayFormatDateTimePicker", new UiDropDownListOptions
{
Placeholder = "Select a format",
Value = "format1",
DataSource = (IEnumerable<CalendarsDemoController.DateTimeFormats>)ViewData["data"],
Fields = new UiDropDownFields { Text = "Text", Value = "Id" },
OnChange = "changeDateTimeFormat"
})
@Html.UiMultiSelect("inputFormatsDateTimePicker", new UiMultiSelectOptions
{
Placeholder = "e.g. MM.dd.yyyy hh:mm a",
Value = new string[] { "MM.dd.yyyy hh:mm a", "yyyy-MM-dd HH:mm" },
DataSource = (string[])ViewData["formatData"],
Mode = UiMultiSelectMode.CheckBox,
ShowSelectAll = true,
OnChange = "changeInputFormats"
})
<style>
#datetimepicker-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="datetimepicker-control">
@Html.UiDateTimePicker("datetimepicker", new UiDateTimePickerOptions
{
Placeholder = "Choose a Date and Time",
ShowClearButton = true,
Value = (DateTime)ViewData["value"],
Format = "dd-MMM-yy hh:mm a",
InputFormats = new string[] { "MM.dd.yyyy hh:mm a", "yyyy-MM-dd HH:mm" }
})
</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("displayFormatDateTimePicker", new UiDropDownListOptions
{
Placeholder = "Select a format",
Value = "format1",
DataSource = (IEnumerable<CalendarsDemoController.DateTimeFormats>)ViewData["data"],
Fields = new UiDropDownFields { Text = "Text", Value = "Id" },
OnChange = "changeDateTimeFormat"
})
</div>
<div class="demo-property-row">
<label class="multi-label">Choose input formats</label>
@Html.UiMultiSelect("inputFormatsDateTimePicker", new UiMultiSelectOptions
{
Placeholder = "e.g. MM.dd.yyyy hh:mm a",
Value = new string[] { "MM.dd.yyyy hh:mm a", "yyyy-MM-dd HH:mm" },
DataSource = (string[])ViewData["formatData"],
Mode = UiMultiSelectMode.CheckBox,
ShowSelectAll = true,
OnChange = "changeInputFormats"
})
</div>
</div>
<script>
function changeDateTimeFormat(value, id) {
var item = window.Lumen.get(id).getItems().filter(function (i) { return i.value === value; })[0];
if (item) { window.Lumen.get('datetimepicker').setFormat(item.text); }
}
function changeInputFormats(values) {
window.Lumen.get('datetimepicker').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 DateTimePickerController : Controller
{
// The page is Views/DateTimePicker/Format.cshtml; it draws what ViewData holds.
public ActionResult Format()
{
ViewData["value"] = DateTime.Now;
ViewData["data"] = new DateTimeFormats().GetDateTimeFormatsWithId();
ViewData["formatData"] = new DateTimeFormats().GetDateTimeInputFormats();
return View();
}
public class DateTimeFormats
{
public string Id { get; set; }
public string Text { get; set; }
public List<DateTimeFormats> GetDateTimeFormatsWithId()
{
return new List<DateTimeFormats>
{
new DateTimeFormats { Id = "format1", Text = "dd-MMM-yy hh:mm a" },
new DateTimeFormats { Id = "format2", Text = "yyyy-MM-dd HH:mm" }
};
}
public string[] GetDateTimeInputFormats()
{
return new string[]
{
"MM.dd.yyyy hh:mm a", "yyyy-MM-dd HH:mm", "dd MMM yyyy HH:mm",
"MMM/dd/yyyy hh:mm tt", "yyyy/MM/dd hh:mm tt", "dd-MM-yyyy HH:mm",
"MM/dd/yyyy hh:mm tt", "yyyy.MM.dd HH:mm"
};
}
}
}
}
@* 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 DateTimePicker component by using the format property. You can also change the date format by selecting it from the format options in the properties panel. By using the timeFormat property to format the displayed time value in the popup list.
Furthermore, this example showcases the flexible date value parsing functionality available in DateTimePicker 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.