piiiias piiiias - 3 months ago 25
Javascript Question

Trying to remove all the passed dates

I have an array with many dates, they are not in the date type but string like: "2016-08-12" for example. Then what I would like to do is to remove all dates that we already have passed. So therefor im trying to compare them to todays date and then remove it if its passed. Using typescript by the way.

my array, named datoArray, looks like this:

["2016-08-02", "2016-08-11", "2016-08-22", "2016-09-10"]

just with a lot more of the same...

then here's what I try to do:

for(var i = 0; i < this.datoArray.length; i++){
this.skoleAar = parseInt(this.datoArray[i].slice(0,4))
this.skoleMaaned = parseInt(this.datoArray[i].slice(5,8))
this.skoleDag = parseInt(this.datoArray[i].slice(8,10))

if(this.skoleAar < dagensAar){
this.datoArray.splice(i, 1);
if(this.skoleAar == dagensAar && this.skoleMaaned < dagensMaaned){
this.datoArray.splice(i, 1);
if(this.skoleAar == dagensAar && this.skoleMaaned == dagensMaaned && this.skoleDag < dagensDag){
this.datoArray.splice(i, 1);

the "dagensAar", "dagensMaaned" and "dagensDag" variables im getting from another function that works. If i "console.log" the variables it prints out int values like 2016 for the year and 8 for the month if i take from the start of the array, and for the "dagensAar", "dagensMaaned" and "dagensDag" it prints 2016 11 20, which is todays year, month and day. all is in Int type, so what im not getting here is why my "if" doesnt work? It seems like there is something wrong with the way i compare the, but i thought this was the way to compare int values?


If the dates are in ISO-8601 format then you can simply filter using Date.parse().

var dates = ["2016-08-02", "2016-08-11", "2016-08-22", "2016-09-10", "2016-12-15"];

function removePastDates(data) {
  var today = new Date();
  console.log('Initial state: ' + data);
  var modified = dates.filter(function(dateString) {
    return Date.parse(dateString) >= today;
  console.log('Final state: ' + modified);
  return modified;

var newDates = removePastDates(dates);