简体   繁体   English

如何显示开始日期在一个月内而结束日期在另一个月内的元素?

[英]How do I show elements that have start date in one month and end date in another?

I am trying to only show elements that are related to certain months. 我试图仅显示与某些月份相关的元素。 I am using PHP to make a select box that value's are the months' first and last days of the month. 我正在使用PHP创建一个选择框,其值是月份的第一天和最后一天。

Here is the resulting HTML for the select box: 这是选择框的结果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>

This is one related element: 这是一个相关的元素:

<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>

As you can see I am using data-start-date and data-end-date to store the start and end date of the element. 如您所见,我正在使用data-start-datedata-end-date来存储元素的开始和结束日期。

Then I am using jQuery to compare the element date range to the select box values. 然后,我使用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' );
        }
    });
}

The issue I am having is with the above element. 我遇到的问题是上述元素。 It has a start date in April and an end date in May. 它的开始日期为4月,结束日期为5月。 This means it should be displayed in both months when selected. 这意味着选择后应在两个月中都显示它。 But instead it only displays in April. 但相反,它仅在4月显示。 What am I missing to make it so elements that have a date range between two or more months display when any of those months are selected? 我缺少什么使之成为日期范围在两个或多个月之间的元素,而在选择这些月份中的任何一个时会显示出来呢?

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

If you're only bothered about if it is in that month then just use the start dates of the month. 如果您只是在那个月才被打扰,那么只需使用该月的开始日期即可。

Change this line 更改此行

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

To

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

It doesn't matter when the month ends you just need to know if the event is ongoing past the 1st of the month 每月的结束时间无关紧要,您只需要知道活动是否在每月的1号之后进行即可

the logic is wrong, try this 逻辑错误,请尝试

$('.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');
    }
});

Edited: final fix: "looks" both way 编辑:最终修正:双向“看起来”

$('.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 http://codepen.io/anon/pen/qEzZvw

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我只需要在开始日期选择的日期选择器中显示未来一个月的日期 - I need to show only future one month dates in the date picker from the start date selection 如何在PHP中设置自定义月份的开始日期和结束日期 - How to set custom month start date and end date in php 如何在JavaScript中获得给定的月份开始日期和结束日期 - How can I get given month start date and end date in javascript 如何在引导程序中比较结束日期​​和开始日期,以确保结束日期大于开始日期 - How do I compare end date with start date in bootstrap to make sure end date is greater than start date 如果我有表格数据,如何在开始日期和结束日期上过滤数据日期? - How to filter data date on start date and end date if i have a tabular data? 如何转换此日期范围(月,年)日期选择器以数字格式显示月份的输出 - How do I convert this date range (month,year) date picker to show the output of the month in numeric format 如何在一个文本字段中显示开始日期和结束日期以及月份和年份应该像下拉菜单 - how to show start date and end date in one text field as well as the months and years should like dropdown 如何定义 Highcharts 甘特图的初始开始和结束日期? - How do I define initial start and end date for Highcharts Gantt? 如何在开始日期和结束日期之间创建日期数组? - HOW do I creating an array of dates between a start and end date? 获取上一年的开始月份/日期和结束月份/日期 - Get previous year start month/date and end month/date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM