2

I am currently working on an ASP.NET MVC 4 project, in my view I am using Kendo UI. I want to display only the month and year from the Datepicker widget(Example: JANUARY 2016) into the alertbox, but instead I am getting the following:IMAGE

My view code is as follows:

 @(Html.Kendo().DatePicker()
               .Name("datepicker")
               .Start(CalendarView.Year)
               .Depth(CalendarView.Year)
               .Format("MMMM yyyy")
               .Value(DateTime.Today)
               .HtmlAttributes(new { style = " width: 95.5%;})
               .Events(e => e.Change("monthpicker_change"))

    )

<script>
   // monthpicker_change function
    function monthpicker_change() {

      var month = $("#datepicker").data("kendoDatePicker");
     alert(month.value());

   }
</script>

Please suggest me what changes I need to do in my script, in order to display only the selected Month and Year in an Alert box.

PS: I have formatted the datepicker to display only the MONTHS and YEAR, not the standard dates

Alchemist
  • 23
  • 1
  • 6

1 Answers1

1

kendoDatePicker.value method always return javascript Date.

You should use Date methods to extract month and year from date object:

var date = $("#datepicker").data("kendoDatePicker").value();
alert((date.getMonth()+1) + '.' + date.getFullYear());

Beware: getMonth() return values from 0 to 11; 0 is january.

Here is full reference of Date functions: http://www.w3schools.com/jsref/jsref_obj_date.asp

Gene R
  • 3,684
  • 2
  • 17
  • 27
  • thank you.It works really well, but is there any method to return the Months as string like(January, February)? – Alchemist Feb 22 '16 at 09:20
  • @Alchemist check http://stackoverflow.com/questions/1643320/get-month-name-from-date – Gene R Feb 22 '16 at 10:07
  • Thanks a lot, instead of array I used a switch. But using an array looks more clean. I will replace the same with array. – Alchemist Feb 22 '16 at 10:15