时刻 js 的无效日期

Invalid Date with moment js

考虑到这一点 http://jsfiddle.net/2a0hsj4z/4/,此处作为片段:

moment.locale("en");

var start = moment("2010-10", "YYYY-MM");
var end = moment("2017-10", "YYYY-MM");

$("#chord_range").ionRangeSlider({
    type: "double",
    grid: true,
    min: start.format("x"),
    max: end.format("x"),
    from: start.format("x"),
    to: end.format("x"),
    prettify: function (num) {
        return moment(num, 'x').format("MMMM YYYY");
    }
});

var slider = $("#chord_range").data("ionRangeSlider");

$(".irs-slider").click(function() {
 console.log(slider.result.from);
})
<link href="http://ionden.com/a/plugins/ion.rangeSlider/static/css/skin2.css" rel="stylesheet"/>
<link href="http://ionden.com/a/plugins/ion.rangeSlider/static/css/ion.rangeSlider.css" rel="stylesheet"/>
<input id="chord_range" />
<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
<script src="http://ionden.com/a/plugins/ion.rangeSlider/static/js/ion-rangeSlider/ion.rangeSlider.js"></script>
<script src="http://momentjs.com/downloads/moment.min.js"></script>

为什么我尝试拖动 left/right 滑块(start/end 值)时得到 Invalid Date error?启动时日期显示正确。当您触摸滑块时,它会断开。 当我用 var start = moment("2012-10", "YYYY-MM"); 替换 var start = moment("2010-10", "YYYY-MM"); 时,它起作用了:

moment.locale("en");

var start = moment("2012-10", "YYYY-MM");
var end = moment("2017-10", "YYYY-MM");

$("#chord_range").ionRangeSlider({
    type: "double",
    grid: true,
    min: start.format("x"),
    max: end.format("x"),
    from: start.format("x"),
    to: end.format("x"),
    prettify: function (num) {
        return moment(num, 'x').format("MMMM YYYY");
    }
});

var slider = $("#chord_range").data("ionRangeSlider");

$(".irs-slider").click(function() {
 console.log(slider.result.from);
})
<link href="http://ionden.com/a/plugins/ion.rangeSlider/static/css/skin2.css" rel="stylesheet"/>
<link href="http://ionden.com/a/plugins/ion.rangeSlider/static/css/ion.rangeSlider.css" rel="stylesheet"/>
<input id="chord_range" />
<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
<script src="http://ionden.com/a/plugins/ion.rangeSlider/static/js/ion-rangeSlider/ion.rangeSlider.js"></script>
<script src="http://momentjs.com/downloads/moment.min.js"></script>

这是为什么?

你错过了 step 选项,step 将为 1,1 对于 (option.max - option.min) 来说太小了,会导致 ion 错误rangeSlider.So 你应该为步骤选项设置一个大数字。

$("#chord_range").ionRangeSlider({
    type: "double",
    grid: true,
    min: start.format("x"),
    max: end.format("x"),
    from: start.format("x"),
    to: end.format("x"),
    step: 100000,
    prettify: function (num) {
      return moment(num, 'x').format("MMMM YYYY");
    }
});