twitter-bootstrap 如何在更改时获取 Bootstrap Datepicker 的年份和月份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29449608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 22:30:50  来源:igfitidea点击:

How to get year and month of Bootstrap Datepicker as it on change

javascriptjquerytwitter-bootstraptwitter-bootstrap-3datepicker

提问by lzc

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

我是这个日期选择器的新手,我不知道如何在更改事件changeMonthchangeYear. 这是插件的链接: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.

问题是 onchange 只捕获从几个月到几年的过渡。我只能获取所选日期的日期/日期,并且在过渡期间没有选择任何内容。

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.

我试图从 div 中抓取文本,但我总是选择一个为时已晚。如何获取转换后所选值的月份和年份。

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

澄清: getYear() 当我们从十年视图更改后看到所有 12 个月时

getMonth() when we see days after the month view

getMonth() 当我们看到月视图后的几天

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

这是我的小提琴:http: //jsfiddle.net/vj3cdzbc/2/

回答by lzc

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];
 });

回答by Navruz Madibragimov

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();               
 });