# Getting Started with the DateTimePicker

This tutorial explains how to set up a basic Telerik UI for ASP.NET Core DateTimePicker and highlights the major steps in the configuration of the component.

You will initialize a DateTimePicker control with a number of tools. Next, you will handle some of the DateTimePicker events. Finally, you can run the sample code in [Telerik REPL](https://netcorerepl.telerik.com/) and continue exploring the components.

![Sample Telerik UI for ASP.NET Core DateTimePicker](https://www.telerik.com/aspnet-core-ui/documentation/assets/b461688c7df121184262e4840ea2b0da/datetimepicker-getting-started.png)

## Prerequisites

To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET Core components:

* You can use the [Telerik REPL](https://netcorerepl.telerik.com/) playground and skip installing the components on your system and configuring a project.

* You can prepare a Visual Studio project by following the following guide:

  * Creating a new pre-configured project for the Telerik UI for ASP.NET Core components from [a project template](https://www.telerik.com/aspnet-core-ui/documentation/vs-integration/new-project-wizard).

## 1. Prepare the CSHTML File

The first step is to add the required directives at the top of the `.cshtml` document:

* To use the Telerik UI for ASP.NET Core HtmlHelpers:

  ```cshtml
  @using Kendo.Mvc.UI
  ```

* To use the Telerik UI for ASP.NET Core TagHelpers:

  ```cshtml
  @addTagHelper *, Kendo.Mvc
  ```

Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and apply some basic styles.

## 2. Initialize the DateTimePicker

Use the DateTimePicker HtmlHelper or TagHelper to add the component to a page:

* The `Name()` configuration method is mandatory as its value is used for the `id` and the `name` attributes of the DateTimePicker element.

* The `Culture()` configuration method specifies the culture info used by the widget. `Date` and `time` values typically vary by culture. For example, the `"d"` standard format string indicates that a date and time value is to be displayed using a short date pattern. For the invariant culture, this pattern is `"MM/dd/yyyy"`. For the `fr-FR` culture, it is `"dd/MM/yyyy"`. For the `ja-JP` culture, it is `"yyyy/MM/dd"`.

* The `Depth` configuration method specifies the navigation depth. The available parameter settings are `month`, `year`, `decade`, and `century`.

```Razor HtmlHelper

@using Kendo.Mvc.UI

    @(Html.Kendo().DateTimePicker()
            .Name("datetimepicker")
            .Value(DateTime.Now)
            .Culture("fr-FR")
            .Depth(CalendarView.Month)
    )
```

```Razor TagHelper
@addTagHelper *, Kendo.Mvc

    <kendo-datetimepicker value="DateTime.Now" name="datetimepicker" culture="fr-FR" depth="CalendarView.Year">
    </kendo-datetimepicker>

```

## 3. Handle the DateTimePicker Events

The DateTimePicker [exposes various events](https://www.telerik.com/aspnet-core-ui/documentation/api/kendo.mvc.ui.fluent/datetimepickereventbuilder) that you can handle and further customize the functionality of the component. In this tutorial, you will use the `Open`, `Close`, and `Change` events of the DateTimePicker.

```Razor HtmlHelper
@using Kendo.Mvc.UI

    @(Html.Kendo().DateTimePicker()
            .Name("datetimepicker")
            .Value(DateTime.Now)
            .Culture("fr-FR")
            .Depth(CalendarView.Month)
              .Events(e =>
              {
                  e.Change("change").Open("open").Close("close");
              })
    )

<script>
    function open(e) {
        console.log("Open :: " + e.view + "-view");
    }

    function close(e) {
        console.log("Close :: " + e.view + "-view");
    }

    function change() {
        console.log("Change :: " + kendo.toString(this.value(), 'g'));
    }
</script>
```

```Razor TagHelper

@addTagHelper *, Kendo.Mvc

        <kendo-datetimepicker value="DateTime.Now" name="datetimepicker" culture="fr-FR" depth="CalendarView.Year" on-change="change" on-open="open" on-close="close">
        </kendo-datetimepicker>

<script>
    function open(e) {
        console.log("Open :: " + e.view + "-view");
    }

    function close(e) {
        console.log("Close :: " + e.view + "-view");
    }

    function change() {
        console.log("Change :: " + kendo.toString(this.value(), 'g'));
    }
</script>
```

For more examples, refer to the [demo on using the events of the DateTimePicker](https://demos.telerik.com/aspnet-core/datetimepicker/events).

## 4. (Optional) Reference Existing DateTimePicker Instances

To use the [client-side API of the DateTimePicker](https://docs.telerik.com/kendo-ui/api/ui/datetimepicker) and build on top of its initial configuration, you need a reference to the DateTimePicker instance. Once you get a valid reference, you can call the respective API methods:

1. Use the `.Name()` (`id` attribute) of the component instance to get a reference.

   ```JS script
       <script>
           var dateTimePickerReference = $("#datetimepicker").data("kendoDateTimePicker"); // DateTimePicker Reference is a reference to the existing instance of the helper.
       </script>
   ```

2. Use the [client-side API of the DateTimePicker](https://docs.telerik.com/kendo-ui/api/ui/datetimepicker) to control the behavior of the widget. In this example, you will use the [`enable`](https://docs.telerik.com/kendo-ui/api/ui/datetimepicker/methods/enable) method to disable the DateTimePicker.

   ```JS script
       <script>
           $(document).ready(function () {
               var dateTimePicker= $("#datetimepicker").data("kendoDateTimePicker");

               dateTimePicker.enable(false);
           })
       </script>
   ```

For more information on referencing specific helper instances, see the [Methods and Events](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/helper-basics/events-and-methods) article.

## Explore this Tutorial in REPL

You can continue experimenting with the code sample above by running it in the Telerik REPL server playground:

* [Sample code with the DateTimePicker HtmlHelper](https://netcorerepl.telerik.com/mdYowWFc11JLqmR846)

* [Sample code with the DateTimePicker TagHelper](https://netcorerepl.telerik.com/GdYyGsFQ129QUP4631)

## Next Steps

* [DateTimePicker Floating Label](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/editors/datetimepicker/floating-label)
* [DateTimePicker Appearance](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/editors/datetimepicker/appearance)
* [DateTimePicker Validation](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/editors/datetimepicker/validation)

## See Also

* [Using the API of the DateTimePicker for ASP.NET Core (Demo)](https://demos.telerik.com/aspnet-core/datetimepicker/api)
* [Client-Side API of the DateTimePicker](https://docs.telerik.com/kendo-ui/api/ui/datetimepicker)
* [Server-Side API of the DateTimePicker](https://www.telerik.com/aspnet-core-ui/documentation/api/datetimepicker)
* [Knowledge Base Section](https://www.telerik.com/aspnet-core-ui/documentation/knowledge-base)
