【发布时间】:2015-11-18 15:21:45
【问题描述】:
出于某种奇怪的原因,MomentJS 范围并不总是显示当前月份。我想知道是否需要设置一个选项,以便将当前月份包含在其日期范围内。
这是用于生成日期范围的代码。我还附上了一个 codepen,以便您可以看到它的实际效果。
http://codepen.io/anon/pen/rORmxY
//momentJs creates the ranges
//Only shows April 2015
var end = moment("2015-05-01","YYYY-MM-DD");
//where the day is > 18 it Shows May 2015
var end2 = moment("2015-05-19","YYYY-MM-DD");
var start = moment().subtract(1.5, "year");
var range = moment.range(start, end);
var range2 = moment.range(start, end2);
//iterate through the months, and populate the SELECT box
range.by('months', function(date) {
//add those values to a SELECT box
$('#month-range').prepend($('<option>', {
value: date.format("MMM YYYY"),
text: date.format("MMM YYYY")
}));
});
//iterate through the months, and populate the SELECT box
range2.by('months', function(date) {
//add those values to a SELECT box
$('#month-range2').prepend($('<option>', {
value: date.format("MMM YYYY"),
text: date.format("MMM YYYY")
}));
});
非常感谢
【问题讨论】:
标签: javascript date momentjs date-range