Filter array of dates

0

I know that there is .filter() that in it I can filter an array to return it just the way I want it, and using a lot to search, when we pass 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 past date I have start date and end date, eg step for 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 all of it to me.

How can I do this?

    
asked by anonymous 17.11.2017 / 17:17

3 answers

1

I was able to find my error, it was {} so then

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);
    
17.11.2017 / 17:38
1

Forgive me for the great gambiarra, 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>
    
17.11.2017 / 17:45
1

You need to convert these strings into numeric 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 to 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);
    
17.11.2017 / 18:18