简体   繁体   English

Jquery 查找日期是否在日期范围之间

[英]Jquery find if date is between date range

So I am trying to find if a certain date is between two other dates and then display the correct div text.所以我试图找出某个日期是否在另外两个日期之间,然后显示正确的 div 文本。 I have it working to a point, but it doesn't seem to work checking multiple divs.我让它工作到一定程度,但它似乎无法检查多个 div。 Heres what I have below, basically it uses the 'date-selected' div and runs through each 'date' div to find the date match.下面是我下面的内容,基本上它使用“日期选择”的 div 并遍历每个“日期”的 div 以找到日期匹配。

It seems to work if the date is 02/01/2019, but if I set the date to 02/01/2020 it will not find the correct div, which should be 02/01/2020-01/01/2021.如果日期是 02/01/2019,它似乎可以工作,但如果我将日期设置为 02/01/2020,它将找不到正确的 div,它应该是 02/01/2020-01/01/2021。 Does anyone know what the problem is?有谁知道问题是什么?

 // on click $(".check").click(function() { // foreach date div $(".date").each(function() { var firstdate = $(this).text().split('-')[0]; var lastdate = $(this).text().split('-')[1]; var fDate, lDate, cDate; fDate = new Date(firstdate); // firstdate lDate = new Date(); lDate.setDate(lDate.getDate(lastdate)); // lastdate cDate = new Date($('.date-selected').text()); // date to check if between if (Date.parse(cDate) <= Date.parse(lDate) && Date.parse(cDate) >= Date.parse(fDate)) { // output matched date $('.correct-date').text('Date between: ' + $(this).text()); return true; } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="date-selected">02/01/2020</div> <div class="date">01/01/2019-01/01/2020</div> <div class="date">02/01/2020-01/01/2021</div> <div class="correct-date"></div> <button class="check">check</button>

not need to new Date , only use Date.parse :不需要new Date ,只使用Date.parse

 $(".check").click(function() { // foreach date div $(".date").each(function() { if ( dateCheck( $(this).text().split('-')[0], $(this).text().split('-')[1], $('.date-selected').text() ) ) { // output matched date $('.correct-date').text('Date between: ' + $(this).text()); return true; } }); }); function dateCheck(from,to,check) { var fDate,lDate,cDate; fDate = Date.parse(from); lDate = Date.parse(to); cDate = Date.parse(check); if((cDate <= lDate && cDate >= fDate)) { return true; } return false; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="date-selected">02/01/2020</div> <div class="date">01/01/2019-01/01/2020</div> <div class="date">02/01/2020-01/01/2021</div> <div class="correct-date"></div> <button class="check">check</button>

You can use getTime() for compare date:您可以使用getTime()比较日期:

 // on click $(".check").click(function() { // foreach date div $(".date").each(function() { var firstdate = $(this).text().split('-')[0]; var lastdate = $(this).text().split('-')[1]; var fDate, lDate, cDate; fDate = new Date(firstdate); // firstdate lDate = new Date(lastdate); //lDate.setDate(lDate.getDate(lastdate)); // lastdate cDate = new Date($('.date-selected').text()); // date to check if between if (cDate.getTime() <= lDate.getTime() && cDate.getTime() >= fDate.getTime()) { // output matched date $('.correct-date').text('Date between: ' + $(this).text()); return true; } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="date-selected">02/01/2020</div> <div class="date">01/01/2019-01/01/2020</div> <div class="date">02/01/2020-01/01/2021</div> <div class="correct-date"></div> <button class="check">check</button>

I have made some modification to your code to look it better.我对您的代码进行了一些修改以使其看起来更好。 Also I have changed the dates.我也更改了日期。

02/01/2020 (Changed)
01/01/2019 - 02/01/2020 (Changed)
02/01/2020 - 01/01/2021

 $(".check").click(function() { $('.correct-date').html(''); $(".date").each(function() { var date = $(this).text(), firstdate = date.split('-')[0], lastdate = date.split('-')[1], fDate = new Date(firstdate).getTime(), lDate = new Date(lastdate).getTime(), cDate = new Date($('.date-selected').text()).getTime(); if (cDate <= lDate && cDate >= fDate) { $('.correct-date').append('Date between: ' + date + "<br>"); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="date-selected">02/01/2020</div> <div class="date">01/01/2019-02/01/2020</div> <div class="date">02/01/2020-01/01/2021</div> <div class="correct-date"></div> <button class="check">check</button>

cDate <= lDate && cDate >= fDate
Date 1 ==> 02/01/2020 <= 02/01/2020 &&  02/01/2020 >= 01/01/2019 == true
Date 2 ==> 02/01/2020 <= 01/01/2021 &&  02/01/2020 >= 02/01/2020 == true

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM