ASP.NET Core DateTimePicker Overview

Telerik UI for ASP.NET Core Ninja image

The DateTimePicker is part of Telerik UI for ASP.NET Core, a professional grade UI library with 110+ components for building modern and feature-rich applications. To try it out sign up for a free 30-day trial.

The Telerik UI DataSource TagHelper and HtmlHelper for ASP.NET Core are server-side wrappers for the Kendo UI DataSource widget.

The DateTimePicker allows the user to select a value from a calendar, a time drop-down list, or through direct input.

Initializing the DateTimePicker

The following example demonstrates how to define the DateTimePicker.

The DateTimePicker copies any styles and CSS classes from the input element to the wrapper element.

    @(Html.Kendo().DateTimePicker()
        .Name("dateTimePicker")
    )
    <kendo-datetimepicker name="dateTimePicker"></kendo-datetimepicker>

Basic Configuration

The DateTimePicker configuration options are passed as attributes.

    @(Html.Kendo().DateTimePicker()
        .Name("end")
        .Value(DateTime.Today)
        .Min(DateTime.Today)
    )
    <kendo-datetimepicker name="end" value="DateTime.Today"
        min="DateTime.Today">
    </kendo-datetimepicker>

The ParseFormats option is of type string[] and can be assigned either by a ViewBag property or by a property of the model.

    @(Html.Kendo().DateTimePicker()
        .Name("datetimepicker")
        .ParseFormats(new string[] { "MMMM yyyy", "MMMM" })
    )
    @{
        ViewBag.ParseDates = new string[] { "MMMM yyyy", "MMMM" };
    }

    <kendo-datetimepicker name="datetimepicker" parse-formats="ViewBag.ParseDates">
    </kendo-datetimepicker>

Functionality and Features

Events

You can subscribe to all DateTimePicker events. For a complete example on basic DateTimePicker events, refer to the demo on using the events of the DateTimePicker.

The following example demonstrates how to subscribe to events by a handler name.

    @(Html.Kendo().DateTimePicker()
      .Name("datetimepicker")
      .Events(e => e
            .Open("datetimepicker_open")
            .Close("datetimepicker_close")
            .Change("datetimepicker_change")
      )
    )
    <kendo-datetimepicker name="datetimepicker"
        on-open="datetimepicker_open"
        on-close="datetimepicker_close"
        on-change="datetimepicker_open"/>
    function datetimepicker_open() {
        // Handle the open event.
    }

    function datetimepicker_close() {
        // Handle the close event.
    }

    function datetimepicker_change() {
        // Handle the change event.
    }

Handling by Template Delegate

The following example demonstrates how to subscribe to events by a template delegate.

    @(Html.Kendo().DateTimePicker()
        .Name("datetimepicker")
        .Events(e => e
            .Open(@<text>
                function() {
                    // Handle the open event inline.
                }
            </text>)
            .Change(@<text>
                function() {
                    // Handle the change event inline.
                }
                </text>)
        )
    )
    <kendo-datetimepicker name="datetimepicker"
        on-open='function(e)
        {
            // Handle the open event inline.
        }'
        on-change='function(e)
        {
            / Handle the change event inline.
        }'/>

Referencing Existing Instances

To reference an existing DateTimePicker instance, use the jQuery.data() configuration option. Once a reference is established, use the DateTimePicker client-side API to control its behavior.

The following example demonstrates how to access an existing DateTimePicker instance.

  // Place the following after your DateTimePicker for ASP.NET Core declaration.
  <script>
  $(function() {
  // The Name() of the DateTimePicker is used to get its client-side instance.
      var datetimepicker = $("#datetimepicker").data("kendoDateTimePicker");
  });
  </script>

See Also

In this article