当用户更改值时,从ION滑块获取日期值

时间:2015-04-17 03:03:39

标签: javascript slider momentjs

我对我的编程问题有疑问,我使用ION Slider(http://ionden.com/a/plugins/ion.rangeSlider/demo_advanced.html)和Moment.js进行日期范围操作。我需要为我的程序设置特定的日期范围和特定的日期范围选择器。

每当用户更改值时,它应该更新数据库。但我在这里有一点问题,滑块在console.log()中没有显示正确的格式。请帮我解决这个问题。我的代码是,

var startDate = moment("2014-01-05");
    var endDate = moment("2014-01-13");

    range.ionRangeSlider({
        type: "double",
        grid: true,

        min: +moment(startDate).format("X"),
        max: +moment(endDate).format("X"),

        from: +moment(endDate).subtract(5, "days").format("X"),
        to: +moment(endDate).subtract(2, "days").format("X"),

        prettify: function (num) {
            return moment(num, "X").format("dddd, MMM Do YYYY");
        }
    });

    range.on("change", function () {
    var $this = $(this),
        from = $this.data("from"),
        to = $this.data("to");

        console.log(from + " - " + to);
    });

控制台日志,

1389113336 - 1389373200
jquery-... > eval (line 28)
1389111677 - 1389373200
jquery-... > eval (line 28)
1389111345 - 1389373200
jquery-... > eval (line 28)
1389109686 - 1389373200
jquery-... > eval (line 28)
1389108359 - 1389373200
jquery-... > eval (line 28)
1389106368 - 1389373200
jquery-... > eval (line 28)
1389103713 - 1389373200
jquery-... > eval (line 28)
1389102054 - 1389373200

它应该在控制台日志中显示日期。

1 个答案:

答案 0 :(得分:4)

我自己找到了答案,

var startDate = moment("2014-01-01");
    var endDate = moment("2014-01-30");

    range.ionRangeSlider({
        type: "double",
        grid: true,
        force_edges: true,

        min: +moment(startDate).format("X"),
        max: +moment(endDate).format("X"),

        from: +moment(endDate).subtract(5, "days").format("X"),
        to: +moment(endDate).subtract(2, "days").format("X"),

        prettify: function (num) {
            return moment(num, "X").format("dddd, MMM Do YYYY");
        }
    });


    range.on("change", function () {
            var $this = $(this), 
                from = $this.data("from"),
                to = $this.data("to");
                console.log( moment(from,"X").format("dddd, MMM Do YYYY") + " - " + moment(to,"X").format("dddd, MMM Do YYYY"))
        });

    });