DateTime Format

27 Mar 20253 minutes to read

DateTime format is a way of representing the date and time value in different string format in textbox.

By default the DateTimePicker’s format is based on the culture. You can also set the own custom format by using the format property.

NOTE

Once the date format property has been defined it will be common to all the cultures.

To know more about the date and time format standards, refer to the Internationalization Date Time Format section.

The following example demonstrates the DateTimePicker with the custom format (yyyy-MM-dd hh:mm).

<ejs-datetimepicker id="datetimepicker" value="@ViewBag.value" format="yyyy-MM-dd hh:mm" placeholder="Enter date and time"></ejs-datetimepicker>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace EJ2CoreSampleBrowser.Controllers
{
    public partial class HomeController : Controller
    {
        // GET: /<controller>/
        public IActionResult DateTimeFormat()
        {
            ViewBag.value = new DateTime(DateTime.Now.Year,DateTime.Now.Month,14);
            return View();
        }
    }
}

Input formats

The inputFormats property in the DatetimePicker control allows users to enter dates and times in various formats, providing flexibility in date and time entry. This property accepts an array of predefined formats that the control recognizes, enabling users to input dates in different ways while ensuring they are parsed correctly.

When the user types the date and time in any of the specified input formats, it will be automatically converted to the display format after pressing Enter, the Tab key, or when the input loses focus. This enhances the user experience by allowing intuitive data entry through various custom input formats.

The following example demonstrates the DateTimePicker with multiple input formats.

<ejs-datetimepicker id="datetimepicker" value="@ViewBag.value" format="yyyy-MM-dd hh:mm" inputFormats="@(new string[] {"MM.dd.yyyy hh:mm a", "yyyy-MM-dd HH:mm"})" placeholder="Enter date and time"></ejs-datetimepicker>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace EJ2CoreSampleBrowser.Controllers
{
    public partial class HomeController : Controller
    {
        // GET: /<controller>/
        public IActionResult DateTimeFormat()
        {
            ViewBag.value = new DateTime(DateTime.Now.Year,DateTime.Now.Month,14);
            return View();
        }
    }
}