Comparing an array of time with a time in javascript

I have an array of time that I get from the database and pass it to the javascript.when I alert the array variable I get the array of times(in my case 2 different times say 12:00 and 14:30),let the array be newdate. In the javascript I also take in the another time say date=(12:00) I want to compare both the timings.both the values in newdate compare with date. How to do it? I tried the following

var newdate = new Array();
newdate = $('#newdate').val();
for(i=0;i<newdate.length;i++) {
    alert(newdate[i]);
}

if (newdate >= date) {
    alert("enable");
}
else {
    alert("disbale");
}

when I ALERT the above I get 1,2,:,0,0 which is wrong,I want 12:00

Upvotes: 0

Views: 459

Answers (1)

user1636522
user1636522

Reputation:

Problem

Consider the following samples :

var string = '12:30';
for (var i = 0; i < string.length; i++) {
    console.log(string[i]); // will print each character separately
}
var array = ['12:30']; // array of strings
for (var i = 0; i < array.length; i++) {
    console.log(array[i]); // will print "12:30"
}

In the first case, replacing '12:30' with ['1', '2', ':', '3', '0'] will give the same result, in other words, looping through a string is like looping through an array of characters. Knowing that, let's return back to your own code :

// newdate is an empty array -> []
var newdate = new Array();
// ... then newdate is overwritten with a string -> "12:00"
newdate = $('#newdate').val();
// ... then newdate is traversed -> ["1", "2", ":", "0", "0"]

Solution

Here is how you could test a time against a collection :

var temp, item, gte;
var time = '12:15';
var collection = ['12:45', '12:00', '12:15', '12:30', '13:00', '11:00'];
var temp = time.split(':');

for (var i = 0; i < collection.length; i++) {
    item = collection[i].split(':');
    gte = +temp[0] >= +item[0];
    if (gte) gte = +temp[1] >= +item[1];
    // if (gte) gte = +temp[2] >= +item[2]; // uncomment to check seconds
    if (gte) alert(time + ' >= item #' + i);
}

The above code will alert three times :

12:15 >= item #1
12:15 >= item #2
12:15 >= item #5

Going further

Reduces the collection to times that pass the function's test :

function filter(collection, fn) {
    var i = 0,
        l = collection.length,
        result = [],
        item;
    for (; i < l; i++) {
        item = collection[i];
        if (fn(item, i)) result.push(item);
    }
    return result;
}

Returns a test function for times greater than or equal to the passed one :

function gte(time) {
    time = time.split(':');
    return function (item) {
        item = item.split(':');
        return +time[0] < +item[0] || (
            +time[0] === +item[0] && +time[1] <= +item[1]
        );
    };
}

Returns a test function for times lower than or equal to the passed one :

function lte(time) {
    time = time.split(':');
    return function (item) {
        item = item.split(':');
        return +time[0] >= +item[0] && +time[1] >= +item[1];
    };
}

Usage examples :

var collection = ['12:45', '12:00', '12:15', '12:30', '13:00', '11:00'];
var gteNoon = filter(collection, gte('12:00'));
var lteNoon = filter(collection, lte('12:00'));
var aroundNoon = filter(collection, function (time) {
    return time.slice(0, 2) === '12';
});

gteNoon;    // ["12:45", "12:00", "12:15", "12:30", "13:00"]
lteNoon;    // ["12:00", "11:00"]
aroundNoon; // ["12:45", "12:00", "12:15", "12:30"]

Upvotes: 1

Related Questions