最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get year and month of Bootstrap Datepicker as it on change - Stack Overflow

programmeradmin1浏览0评论

I'm new to this datepicker and I don't know how to get the month and year string on change events changeMonth and changeYear. Here is a link to the plugin:

The issue is that onchange captures only the transition from months to years. I am only able to get the day/date of the selected date, and nothing is selected during transition.

I tried to grab text from the div, but I am always one selection too late. How can I get the month and year of the value that is selected after a transition.

To clarify: getYear() when we see all 12 months after changing from the decade view

getMonth() when we see days after the month view

Heres my fiddle: /

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

The issue is that onchange captures only the transition from months to years. I am only able to get the day/date of the selected date, and nothing is selected during transition.

I tried to grab text from the div, but I am always one selection too late. How can I get the month and year of the value that is selected after a transition.

To clarify: getYear() when we see all 12 months after changing from the decade view

getMonth() when we see days after the month view

Heres my fiddle: http://jsfiddle.net/vj3cdzbc/2/

Share Improve this question edited Apr 4, 2015 at 18:10 lzc asked Apr 4, 2015 at 17:52 lzclzc 1,7055 gold badges27 silver badges42 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

Get month:

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

Get year

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

Get Month and Year

 $("#datepicker").datepicker().on('changeDate', function (e) {
    var pickedMonth = new Date(e.date).getMonth() + 1;
    var pickedYear = new Date(e.date).getFullYear();               
 });
发布评论

评论列表(0)

  1. 暂无评论