【问题标题】:How do I show elements that have start date in one month and end date in another?如何显示开始日期在一个月内而结束日期在另一个月内的元素?
【发布时间】:2015-04-08 19:21:46
【问题描述】:

我试图只显示与特定月份相关的元素。我正在使用 PHP 制作一个选择框,该框的值是月份的第一天和最后一天。

这里是选择框的 HTML:

<select id="month" name="monthdaterange">
    <option value="1427864400,1430370000">April</option>
    <option value="1430456400,1433048400">May</option>
    <option value="1433134800,1435640400">June</option>
    <option value="1435726800,1438318800">July</option>
    <option value="1438405200,1440997200">August</option>
    <option value="1441083600,1443589200">September</option>
    <option value="1443675600,1446267600">October</option>
    <option value="1446354000,1448863200">November</option>
    <option value="1448949600,1451541600">December</option>
    <option value="1451628000,1454220000">January</option>
    <option value="1454306400,1456725600">February</option>
    <option value="1456812000,1459400400">March</option>
</select>

这是一个相关的元素:

<div data-start-date="1429765200" data-end-date="1431061200" class="small-12 medium-6 large-6 columns special-container">
    <div class="special-block">
        <div class="special-block-text">
            <h3>Special Test 1</h3>
            <p>Lorem ipsum dolor sit amet....</p>
        </div>
    </div>
</div>

如您所见,我使用data-start-datedata-end-date 来存储元素的开始和结束日期。

然后我使用 jQuery 将元素日期范围与选择框值进行比较。

function get_month_specials(mnth){
    var daterange = mnth.split(',');
    var firstofmonth = daterange[0];
    var lastofmonth = daterange[1];
    //alert('The start date of this month is: ' + firstofmonth + '. The end date of this month is: ' + lastofmonth);

    jQuery('.special-container').each( function( index, value ){
        if(jQuery(this).attr('data-start-date') >= firstofmonth || jQuery(this).attr('data-end-date') <= lastofmonth){
            jQuery(this).css( 'background-color', '#fff' );
        }
    });
}

我遇到的问题与上述元素有关。它的开始日期为 4 月,结束日期为 5 月。这意味着它应该在选择后的两个月中显示。但相反,它只在四月显示。我缺少什么让日期范围介于两个或多个月份之间的元素在选择这些月份时显示?

这是一个 Codepen:http://codepen.io/anon/pen/wBLwwv

【问题讨论】:

    标签: javascript jquery html date


    【解决方案1】:

    如果您只关心是否在该月,那么只需使用该月的开始日期。

    改变这一行

    if(jQuery(this).attr('data-start-date') >= firstofmonth || jQuery(this).attr('data-end-date') >= lastofmonth){
    

    if(jQuery(this).attr('data-start-date') >= firstofmonth || jQuery(this).attr('data-end-date') >= firstofmonth ){
    

    月末何时结束并不重要,您只需要知道该事件是否持续到当月 1 日之后

    【讨论】:

    • 谢谢你这似乎是在做的伎俩,看了之后我已经为这个逻辑设置了它,但我似乎混淆了我需要最后一个月。
    • 经过进一步测试,这还不是 100% 有效。如果我只有 7 月的特价,它会显示选择 4 月、5 月、6 月和 7 月的时间?我只希望这个特别节目在七月上映。有什么想法吗?
    【解决方案2】:

    逻辑错了,试试这个

    $('.special-container').each( function( index, value ){
        $(this).addClass('hidden');
        if(
          ($(this).attr('data-start-date') >= firstofmonth && $(this).attr('data-start-date') <= lastofmonth) ||
          ($(this).attr('data-end-date') <= lastofmonth && $(this).attr('data-end-date') >= firstofmonth)) {
            $(this).removeClass('hidden');
        }
    });
    

    已编辑:最终修复:双向“看起来”

    $('.special-container').each( function( index, value ){
      $(this).addClass('hidden');
      if( 
         (firstofmonth >= $(this).attr('data-start-date') && firstofmonth <= $(this).attr('data-end-date')) || 
         (lastofmonth >= $(this).attr('data-start-date') && lastofmonth <= $(this).attr('data-end-date')) ||
         ($(this).attr('data-start-date') >= firstofmonth && $(this).attr('data-start-date') <= lastofmonth)
      ) {
        $(this).removeClass('hidden');
      }
    });
    

    http://codepen.io/anon/pen/qEzZvw

    【讨论】:

    • 我试过用这个,但是如果特别的日期是 2015 年 4 月 1 日到 2016 年 4 月 1 日,那么特别节目只会在 4 月而不是在任何选择中显示。
    • 你能提供那个特殊日期的样品吗?
    • $('.special-container').each( function( index, value ){ $(this).addClass('hidden'); if( (firstofmonth >= $(this). attr('data-start-date') && firstofmonth = $(this).attr('data-start-date' ) && lastofmonth
    • 这是我从评论和其他示例中添加您的代码的 Codepen。我仍然没有看到我正在寻找的行为。 codepen.io/anon/pen/OPeNvM
    • 时间戳与文本有问题,请先仔细检查
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 2019-02-25
    • 1970-01-01
    • 2019-07-10
    相关资源
    最近更新 更多