Filter array of dates

Asked

Viewed 632 times

0

Well I know there’s a .filter() that in it I can filter an array to return it just the way I want, and using a lot for research, when we spend what we want to search in that array.

But 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 last date I have start date and end date, for example step to it "09:00" and "11:30" he shall return to me:

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

Trying to do this I did so in mine 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 receive all the times I have, then on line 2,4 I filter this array, but it returns me it all.

How can I do this ?

  • you don’t need to use the return before the: item > this.hourNow... ?

3 answers

1

I managed to find my mistake, it was the {} then so

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);

  • 2

    The way you were doing it is also possible, just put the return before instruction.

1

Forgive me for the great gambit, but if it helps:

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

var arrCondition = ["09:00", "10:30"];

var arrFinal = [];

var contador = 0;

var separaCondition1 = arrCondition[0].split(":");
var separaCondition2 = arrCondition[1].split(":");

arrData.filter(function(index){
    var separaData = index.split(":");
    if(parseInt(separaData[0]) >= parseInt( separaCondition1[0]) && parseInt(separaData[0]) <= parseInt(separaCondition2[0])){
      if(parseInt(separaData[1]) >= parseInt(separaCondition1[1]) && parseInt(separaData[1]) <= parseInt(separaCondition2[1])){
        arrFinal[contador] = separaData[0]+":"+separaData[1];
        contador++;
      }
    }
});

console.log(arrFinal);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1

You have to convert those strings into numerical values or dates in order to work them.

Two suggestions:

Converting in seconds

function hhmmToMin(str) {
  const [h, m] = str.split(':').map(Number);
  return h * 60 + m;
}

function filtrarHoras(de, ate, arr) {
  de = hhmmToMin(de);
  ate = hhmmToMin(ate);
  return arr.filter(str => {
    const minutos = hhmmToMin(str);
    return minutos > de && minutos < ate;
  });
}

const horas = ["08:00", "09:00", "10:10", "10:30", "10:50", "11:30", "11:50", "12:00"];


const res = filtrarHoras("09:00", "11:30", horas);
console.log(res);

Converting into dates (milliseconds)

function hhmmToMin(str) {
  const [h, m] = str.split(':').map(Number);
  return new Date(2010, 1, 1, h, m);
}

function filtrarHoras(de, ate, arr) {
  de = hhmmToMin(de);
  ate = hhmmToMin(ate);
  return arr.filter(str => {
    const minutos = hhmmToMin(str);
    return minutos > de && minutos < ate;
  });
}

const horas = ["08:00", "09:00", "10:10", "10:30", "10:50", "11:30", "11:50", "12:00"];


const res = filtrarHoras("09:00", "11:30", horas);
console.log(res);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.