Getting Started with ASP.NET Core TimePicker Control

23 Jul 20264 minutes to read

This section briefly explains how to include the ASP.NET Core TimePicker control in an ASP.NET Core application using Visual Studio.

Create ASP.NET Core web application with Razor pages

Create an ASP.NET Core Web App using Visual Studio via Microsoft Templates or the ASP.NET Core Extension. For detailed instructions, refer to the ASP.NET Core Web App Getting Started documentation.

Install ASP.NET Core package in the application

To add ASP.NET Core TimePicker control in the app, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search and install the Syncfusion.AspNetCore.Calendars and Syncfusion.AspNetCore.Themes packages. All Syncfusion ASP.NET Core packages are available in nuget.org. See the NuGet packages topic for details.

Alternatively, you can install the same package using the Package Manager Console with the following command.

Install-Package Syncfusion.AspNetCore.Calendars -Version 34.1.29
Install-Package Syncfusion.AspNetCore.Themes -Version 34.1.29

Add the ASP.NET Core Tag Helper

After the packages are installed, open the ~/Pages/_ViewImports.cshtml file and import the Syncfusion.AspNetCore.Base and Syncfusion.AspNetCore.Calendars Tag Helpers.

@addTagHelper *, Syncfusion.AspNetCore.Base
@addTagHelper *, Syncfusion.AspNetCore.Calendars

Add stylesheet and script resources

The theme stylesheet and script can be referenced from NuGet through Static Web Assets. Include the stylesheet and script references inside the <head> of ~/Pages/Shared/_Layout.cshtml file.

<head>
    ...
    <link rel="stylesheet" href="_content/Syncfusion.AspNetCore.Themes/styles/fluent2.css" />
    <script src="_content/Syncfusion.AspNetCore.Calendars/scripts/sf-timepicker.min.js"></script>
</head>

Register the Script Manager

Open the ~/Pages/Shared/_Layout.cshtml file and register the script manager <ejs-scripts> at the end of the <body> element as follows.

<body>
    ...
    <!-- Syncfusion ASP.NET Core Script Manager -->
    <ejs-scripts></ejs-scripts>
</body>

Add ASP.NET Core TimePicker control

Add the ASP.NET Core TimePicker control in the ~/Pages/Index.cshtml file.

<ejs-timepicker id="timepicker"></ejs-timepicker>

Run the application

Press Ctrl+F5 (Windows) or +F5 (macOS) to launch the application. The ASP.NET Core TimePicker will render in your default web browser.

ASP.NET Core TimePicker Control

Setting the value within min and max time

The following example demonstrates how to set the value, min and max time on initializing the TimePicker. The TimePicker allows you to select the time value within a range from 1:00 AM to 11:00 AM.

@{
    
    var minVal = new DateTime(2022, 05, 07, 1, 00, 00);
    var maxVal = new DateTime(2022, 05, 07, 11, 00, 00);
    var value = new DateTime(2022, 05, 07, 4, 00, 00);
}

<ejs-timepicker id="timepicker" value="value" min="minVal" max="maxVal"></ejs-timepicker>

ASP.NET Core Timepicker with Minumum and Maximum Time

Setting the time format

Time formats is a way of representing the time value in different string format in textbox and popup list. By default, the TimePicker’s format is based on the culture. You can also customize the format by using the format property. To know more about the time format standards, refer to the Date and Time Format section.

The following example demonstrates the TimePicker control in 24 hours format with 60 minutes interval. The time interval is set to 60 minutes by using the step property.

@{
    
    var value = new DateTime(2022, 05, 07, 4, 00, 00);
}

<ejs-timepicker id="timepicker" format="HH:mm" step="60" value="value"></ejs-timepicker>

ASP.NET Core TimePicker with Format

NOTE

View Sample in GitHub.

See also