Hayu Rahiza
Hayu Rahiza

Reputation: 345

How to compare time between night time to morning time in javascript?

I need to compare a time between a several time range. And one of the range is from night time to morning time for example 2200-0459. How to do this in Java Script? Currently I just manage to compare from morning to evening or evening to night time e.g. 0500-0759.

Here is the code:

function setRowBGColor()
    {
        var time = $("#DrugTime option:selected").text().replace(':', '');
        $("#TimeSlot").find('tr').each(function (i) {
            var $tds = $(this).find('td'),
                timeslot = $tds.eq(0).text(),
                timerange = $tds.eq(1).text().split('-'); 
            if(time >= timerange[0] && time <= timerange[1])
            {
                alert('Row ' + (i + 1) + ':\nTime Slot: ' + timeslot + '\Time Range: ' + timerange[0] + '-' + timerange[1]);
            }
        });
    }

And here is the fiddle: http://jsfiddle.net/5qGfx/

Upvotes: 0

Views: 831

Answers (3)

aravind
aravind

Reputation: 2877

Added a few twists to the one by @Johan

  1. Checking for numbers is better than checking with strings.
  2. Keeping the if check simple than putting in all the logic inside that check

http://jsfiddle.net/aravindbaskaran/VWXrD/

function setRowBGColor() {
    var rows = $("#TimeSlot").find('tr');
    var lowestBound = parseInt($(rows[1]).find("td").eq(1).text().split('-')[0]);
    var time = $("#DrugTime option:selected").text().replace(':', '');
    time = parseInt(time, 10);
    if(time < lowestBound){
        time += 2400;
    }
    rows.each(function (i) {
        var $tds = $(this).find('td'),
            timeslot = $tds.eq(0).text(),
            timerange = $tds.eq(1).text().split('-');
        timerange[0] = parseInt(timerange[0],10);
        timerange[1] = parseInt(timerange[1],10);
        if(timerange[1] < timerange[0]){
            timerange[1] += 2400;
        }
        if (!isNaN(timerange[0])&& time >= timerange[0] && time <= timerange[1]) {
            alert('Row ' + (i - 1) + ':\nTime Slot: ' + timeslot + '\nTime Range: ' + timerange[0] + '-' + timerange[1]);
        }
    });
}

UPDATE : Improved if checks

Upvotes: 1

Eros Anwar
Eros Anwar

Reputation: 26

I think there is a typo for range 6. I assume it should have been 1500-1859.

function setRowBGColor() {
    var time = parseInt($("#DrugTime option:selected").text().replace(':', ''));
    $("#TimeSlot").find('tr').each(function (i) {
        var $tds = $(this).find('td'),
            timeslot = $tds.eq(0).text(),
            timerange = $tds.eq(1).text().split('-'),
            lowerBound = parseInt(timerange[0]),
            upperBound = parseInt(timerange[1]);

        // Fix time for compare.
        if (upperBound < lowerBound) {
            upperBound += 2400;

            if (!((time >= lowerBound) && (time <= upperBound)))
                time += 2400;
        }

        if (time >= lowerBound && time <= upperBound) {
            alert('Row ' + (i + 1) + ':\nTime Slot: ' + timeslot + '\nTime Range: ' + timerange[0] + '-' + timerange[1]);
        }
    });
}

http://jsfiddle.net/Q4dz5/2/

Upvotes: 1

C3roe
C3roe

Reputation: 96226

Simple: If the end time of the range is lower than the start time (that is that case if the interval is spanning around midnight) – then it is enough, if the time is either greater than (equal to) the start time, or lesser than (equal to) the end time:

if (
  time >= timerange[0] && time <= timerange[1]
  ||
  timerange[1] < timerange[0] && (time >= timerange[0] || time <= timerange[1])
) 

http://jsfiddle.net/5qGfx/2/

Upvotes: 1

Related Questions