# Getting Started with the DatePicker

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

You will initialize a DatePicker component and set a default value.

![Sample Telerik UI for ASP.NET MVC DatePicker](https://www.telerik.com/aspnet-mvc/documentation/assets/cda95339923e4633ac5be16c740cd4a7/datepicker-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 MVC components:

* To create a new pre-configured project for the Telerik UI for ASP.NET MVC components, you can [use a project template](https://www.telerik.com/aspnet-mvc/documentation/getting-started-mvc/first-steps).

* To manually configure an existing project by using NuGet, see the [Adding Telerik UI through NuGet](https://www.telerik.com/aspnet-mvc/documentation/getting-started/setup-with-nuget).

## 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 MVC HtmlHelpers:

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

## 2. Initialize the DatePicker

Use the DatePicker HtmlHelper  to add the component to the page:

* The `Name()` configuration method is mandatory as its value is used for the `id` and the `name` attributes of the DatePicker element.
* The `Label()` configuration specifies the label text of the DatePicker.

> Do not set the `Name()` option when using `DatePickerFor`. The `[ComponentName]For` method automatically sets the control's `Name()` to the field it is bound to. For more information, see the [Fundamentals](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/fundamentals-mvc) article.

```Razor HtmlHelper

	@(Html.Kendo().DatePicker()
		.Name("datepicker")
        .Label(label=>{
            label.Content("DatePicker:");
        })
	)
```

## 3. Set a Default Value

The next step is to set a default value. The following example shows how to use the `.Value()` method of the DatePicker, to set the current date as a value of the component.

```Razor HtmlHelper
    @{
       var defaultDatePicker = DateTime.Now;
    }

	@(Html.Kendo().DatePicker()
		.Name("datepicker")
        .Label(label=>{
            label.Content("DatePicker:");
        })
        .Value(defaultDatePicker)
	)
```

## 4. Handle the DatePicker Events

The DatePicker component exposes convenient events for implementing custom logic. In this example, you will use the Change() event to log the value of the selected item in the browser's console.

```Razor HtmlHelper
    @{
       var defaultDatePicker = DateTime.Now;
    }

	@(Html.Kendo().DatePicker()
		.Name("datepicker")
        .Label(label=>{
            label.Content("DatePicker:");
        })
        .Value(defaultDatePicker)
        .Events(e=>e.Change("onChange"))
	)
```

```JavaScript
    function onChange(e){
        var datePicker = e.sender;
        console.log(datePicker.value());
    }
```

## 5. (Optional) Reference Existing DatePicker Instances

To use the client-side API of the DatePicker and build on top of its initial configuration, you need a reference to the DatePicker instance:

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

   ```JS script
       <script>
           $(document).ready(function() {
               var datepickerReference = $("#datepicker").data("kendoDatePicker"); // datepickerReference is a reference to the existing DatePicker instance of the helper.
           })
       </script>
   ```

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

   ```JS script
       <script>
           $(document).ready(function() {
               var datepicker = $("#datepicker").data("kendoDatePicker");

               //Use the "value" API method to get the DatePicker's value.
               console.log(datepicker.value());
           })
       </script>
   ```

## Next Steps

* [Subscribing to DatePicker Events](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/datepicker/events)
* [Customizing the Appearance of the DatePicker](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/datepicker/appearance)

## See Also

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