如何将html时间与当前时间进行比较并更改颜色

时间:2018-01-28 13:28:18

标签: javascript html

所以我在td标签中有一个不同时间的表。 我想将tds中的时间与当前时间进行比较,具体取决于我想通过javascript更改颜色:

你能告诉我在代码底部的if参数/比较中我需要输入什么内容吗?

<div class="container">
    <div class="row">
        <div class="col-lg-12">
            <table class="table">
                <thead>
                    <tr>
                        <th>FlightNr</th>
                        <th>from</th>
                        <th>scheduled</th>
                        <th>Gate</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>KP7809</td>
                        <td>Queilén</td>
                        <td class="time">06:30</td>
                        <td>M13</td>
                    </tr>
                    <tr>
                        <td>BR2168</td>
                        <td>Gargazzone/Gargazon</td>
                        <td class="time">07:45</td>
                        <td>N12</td>
                    </tr>
                    <tr>
                        <td>GN5746</td>
                        <td>Surat</td>
                        <td class="time">08:50</td>
                        <td>X58</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

<script>
    var now = new Date();
    var h = now.getHours();
    var m = now.getMinutes();
    var x = document.getElementsByClassName("time");
    for (var i = 0; i < x.length; i++) {
        if(time in td tags are older than current time)
        x[i].style.color ="red";
    }
</script>

3 个答案:

答案 0 :(得分:1)

您必须将值从字符串转换为int。在此之后,您可以将其与当前时间进行比较。因此,你的循环看起来像:

var hoursInTd = parseInt(x[i].innerText.split(':')[0]) 
var minutesInTd = parseInt(x[i].innerText.split(':')[1])

if (hoursInTd > h) {
   // hours in td are later
   x[i].style.color ="red";
} else if (hoursInTd === h && minutesInTd > m) {
   // hours are same, but the minutes are later
   x[i].style.color ="red";
}

希望它有所帮助。

答案 1 :(得分:1)

我使用时间戳来比较当前日期和您想要的日期。 请注意,我假设桌子上的日期是在今天的同一天。欢呼声。

var now = new Date();   
var timeContainers = document.getElementsByClassName("time");

for (var i = 0; i < timeContainers.length; i++) {
   var [hours,minutes] = timeContainers[i].innerHTML.split(':');
   var dateToCompare = new Date(now.getFullYear(),now.getMonth(),now.getDate(),Number(hours),Number(minutes))
    if(now.valueOf() - dateToCompare.valueOf() > 0){
      timeContainers[i].style.color='red';
    }
}
<div class="container">
    <div class="row">
        <div class="col-lg-12">
            <table class="table">
                <thead>
                    <tr>
                        <th>FlightNr</th>
                        <th>from</th>
                        <th>scheduled</th>
                        <th>Gate</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>KP7809</td>
                        <td>Queilén</td>
                        <td class="time">06:30</td>
                        <td>M13</td>
                    </tr>
                    <tr>
                        <td>BR2168</td>
                        <td>Gargazzone/Gargazon</td>
                        <td class="time">07:45</td>
                        <td>N12</td>
                    </tr>
                    <tr>
                        <td>GN5746</td>
                        <td>Surat</td>
                        <td class="time">08:50</td>
                        <td>X58</td>
                    </tr>
                    
                     <tr>
                        <td>GN5746</td>
                        <td>Surat</td>
                        <td class="time">18:50</td>
                        <td>X58</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

答案 2 :(得分:0)

将时间转换为日期对象,并将其与当前日期进行比较。

&#13;
&#13;
var now = new Date();
    var x = document.getElementsByClassName("time");
    for (var i = 0; i < x.length; i++) {
        var time = new Date(Date.parse('01/01/2011'+ ' ' + x[i].innerHTML))
        if(time < now){
        console.log(x[i].innerHTML)
            x[i].style.color = "red";
        }
        
    }
&#13;
<div class="container">
    <div class="row">
        <div class="col-lg-12">
            <table class="table">
                <thead>
                    <tr>
                        <th>FlightNr</th>
                        <th>from</th>
                        <th>scheduled</th>
                        <th>Gate</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>KP7809</td>
                        <td>Queilén</td>
                        <td class="time">06:30</td>
                        <td>M13</td>
                    </tr>
                    <tr>
                        <td>BR2168</td>
                        <td>Gargazzone/Gargazon</td>
                        <td class="time">07:45</td>
                        <td>N12</td>
                    </tr>
                    <tr>
                        <td>GN5746</td>
                        <td>Surat</td>
                        <td class="time">08:50</td>
                        <td>X58</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;