Renan Rodrigues
Renan Rodrigues

Reputation: 306

Filter array of dates

I know that there is .filter () in it that filters an array to return it just the way I want, and using a lot to search, when we pass what we want to search in that array.

However I'm having trouble filtering an array of dates like this:

["08:00" "09:00" "10:10" "10:30" "10:50" "11:30" "11:50" "12:00"];

I need to filter it by past date I have start date and end date, for example step to it " 09:00 " and " 11:30 " it should return to me:

["10:10" "10:30" "10:50"]

Trying to do this I did so in my typescript:

1. this.schedules = this.navigation.lineSelected.schedules;
2. this.schedules.filter (item => {
3. item> this.hourNow && item <this.hourFinish
4.});

On line 1 I get all the times I have, so on line 2.4 I filter this array, but it returns everything to me.

How can I do this?

Translating what I need:

I'm going to have two strings of hours like "12:00" and "15:00" I need to make comparisons in my array, because I need the hours that are between interval as in the above example.

When I say time I'm talking about a string, so I need to compare string if it's bigger or not than the other type string that are below "12:00" will not appear in my final array, and strings larger than "15:00" will also do not.

This is the best way I can explain what I need, I do not see any other option to explain. Simply filter an array

Upvotes: 3

Views: 13198

Answers (5)

Tutch
Tutch

Reputation: 289

If you want to do it in a more archaic way without using filter or Date you can use this. I'd personally use Nina's answer.

var timestamps = ["08:00","09:00","10:10","10:30","10:50","11:30","11:50","12:00"];

function compareTimeString(time1, time2) {
    time1 = time1.split(":");
    time2 = time2.split(":");

    time1 = parseInt(time1[0]) * 60 + parseInt(time1[1]);
    time2 = parseInt(time2[0]) * 60 + parseInt(time2[1]);

    return time1 > time2 ? true : false;
}

function middleTime(startTime,endTime, timestamps) {
    var times = []

    for(i=0; i<timestamps.length; i++) {
        if(!compareTimeString(startTime,timestamps[i]) && 
            compareTimeString(endTime,timestamps[i])) {
            times.push(timestamps[i])
        }
    }

    return times;
}

Upvotes: 1

davidkonrad
davidkonrad

Reputation: 85578

You could use a combination of new Date() and valueOf() to compare the timestamps, it seems to me the actual date is irrelevant :

function filterTimes(arr, min, max) {
   min = new Date('01-01-2017 '+min).valueOf();
   max = new Date('01-01-2017 '+max).valueOf();
   return arr.filter(function(a) {
      var d = new Date('01-01-2017 '+a).valueOf();
      if (d>min && d<max) return a
   })
}

Usage filterTimes(array, '09:00', '11:30')

here is a demo -> http://jsfiddle.net/u3g4u256/

Upvotes: 0

Vipin Kumar
Vipin Kumar

Reputation: 6546

Please find working solution below. Enjoy :)

var dates = ["08:00", "09:00", "10:10", "10:30", "10:50", "11:30", "11:50", "12:00"]

function parseDate(dStr) {
  var now = new Date();
  now.setHours(dStr.split(':')[0]);
  now.setMinutes(dStr.split(':')[1]);
  now.setSeconds(0);
  return now;
}

function filterDates(start, end) {
  var startDate = parseDate(start);
  var endDate = parseDate(end);
  var parsedDates = dates.map(parseDate);
  return parsedDates.filter(function(dt) {
    return (dt > startDate && dt < endDate) 
  }).map(function(dt) {
    return dt.getHours() + ":" + dt.getMinutes()
  })
}

var result = filterDates("09:00", "11:30");
console.log(result);

Upvotes: 0

sumeet kumar
sumeet kumar

Reputation: 2648

I would suggest using Moment as below and parse it to date.

var str = '15:16:33';
var d = new moment(str, 'HH:mm:ss');

Here is more details on moment formats http://momentjs.com/docs/#/parsing/string-format/

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386868

You could just filter the values with a trimmed min and max value.

var array = ["08:00", "09:00", "10:10", "10:30", "10:50", "11:30", "11:50", "12:00"],
    min = "09:00",
    max = "11:30",
    result = array.filter(a => a > min && a < max);

console.log(result);

Upvotes: 5

Related Questions