How to get the year and month of Bootstrap Datepicker as it changes

I'm new to this datepicker, and I don't know how to get the month and year string in changeMonth and changeYear change events. Here is the link to the plugin: https://github.com/eternicode/bootstrap-datepicker

The problem is that onchange only captures the transition from a few months to years. I can only get the day / date of the selected date, and nothing is selected during the transition.

I tried to grab text from a div, but I'm always one of them too late. How can I get the month and year of the value selected after the transition.

To clarify: getYear () when we see all 12 months after moving from a ten-year review

getMonth () when we see the days after viewing the month

Here is my fiddle: http://jsfiddle.net/vj3cdzbc/2/

+5
source share
1 answer

Get a month:

  $("#datepicker").datepicker().on('changeMonth', function(e){ var currMonth = new Date(e.date).getMonth() + 1; }); 

Get a year

  $("#datepicker").datepicker().on('changeYear', function(e){ var currYear = String(e.date).split(" ")[3]; }); 
+7
source

All Articles