【问题标题】:MomentJS range not always including current month in date rangeMomentJS 范围并不总是包括日期范围内的当前月份
【发布时间】: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


    【解决方案1】:

    我发现的解决方法是简单地告诉 momentJs 使用月末作为范围。在这种情况下这是有道理的。这是通过将 .endOf("month") 添加到结束范围来完成的。

    请参阅此代码以进行说明:

    //momentJs creates the ranges
    //adding .endOf("month") will Include the month of May in the month ranges
    var end = moment("2015-05-01","YYYY-MM-DD").endOf("month");
    var start = moment().subtract(1.5, "year");
    var range = moment.range(start, end); 
    
    //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")
      }));
    });
    

    非常感谢您的帮助

    【讨论】:

      【解决方案2】:

      请查看月份范围的源代码(https://github.com/gf3/moment-range/blob/master/lib/moment-range.js)。

      将结束日期设置为 2015-05-18 将减少一个月,因为 2015-05-19 没有什么特别的。这是因为今天是 2015 年 11 月 18 日,而您将开始日期设置为 2014 年 5 月 18 日。

      现在看一下矩范围内的 _byString 函数(由 'by' 函数调用)。

      function _byString(interval, hollaback, exclusive) {
        var current = moment(this.start);
      
        while (this.contains(current, exclusive)) {
          hollaback.call(this, current.clone());
          current.add(1, interval);
        }
      }
      

      “当前”变量从“2014-05-18”开始,每次增加一个月。 如果结束日期是 2015-05-19,则当 'current' = '2015-05-18' 时,'this.contains()' 只会返回 true。如果结束日期是 2015-05-19,“this.contains”将返回 false。这就是您的代码行为的原因。

      现在,如果您想始终包含当前月份,我认为您始终将结束日期添加 1 天。但是,我认为这种方法可能会在一个月的最后一天引起一些问题。或者,您始终可以将结束日期设置为下个月的第一天,但​​不包括该月。你可以做一些实验。

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-25
        • 2014-01-07
        相关资源
        最近更新 更多