Receive an array of dates different from the one chosen

1

I have a form, where I need to choose the dates, which in this case would be multiple.

I'm using this plugin / JQuery component: link

My problem is that I do not want to save the selected dates, but the ones that are not marked.

Very simple example:

  

I choose 10/10/2016

I have to receive in my input text:

  

01/10/2016, 10/02/2016 ... with the exception of 10/10/2016.

Code:
link

    
asked by anonymous 22.10.2016 / 00:52

2 answers

2

Cycles to run all dates in the given month and filters out the ones you do not want.

For this you need a onSelect in the plugin options, as callback of the change of values.

It would look like this:

$('#datePick').multiDatesPicker({
  dateFormat: "yy-m-d", // para ter ano com 4 casas
  onSelect: function(str) {
    var arrayDeDatas = this.value.split(',').map(function(str) {
      return str.trim();
    });
    var invertidas = inverterEscolha(arrayDeDatas);
    alert(JSON.stringify(invertidas, null, 4)); // só para o exemplo
  }
});

function dataFormatada(d) {
  return [d.getFullYear(), d.getMonth() + 1, d.getDate()].join('-');
}

function inverterEscolha(datas) {
  var inicio = datas.slice(0, 1)[0].split('-').map(Number);
  var fim = datas.slice(-1)[0].split('-').map(Number);
  var data = new Date(inicio[0], inicio[1] - 1); // esta data vai sendo mudada dentro do loop
  var limite = new Date(fim[0], fim[1], 0); // ultimo dia do mês escolhido
  var datasInvertidas = [];
  var contador = 0;
  while (data < limite) {
    contador++;
    data = new Date(inicio[0], inicio[1] - 1, contador);
    var str = dataFormatada(data);
    if (datas.indexOf(str) == -1) datasInvertidas.push(data);
  }
  return datasInvertidas.map(dataFormatada);
}

jsFiddle: link

    
22.10.2016 / 09:25
0

You can check the click of your submit button, in case you change a link (), get the dates that were selected in the input and check them in a loop the dates you want to capture, at the end call submit form . Below is a function to check the last day of the month that can help with this logic.

    function daysInMonth(month,year) {
        var m = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
       if (month != 2) return m[month - 1];
       if (year % 4 != 0) return m[1];
       if (year % 100 == 0 && year%400 != 0) return m[1];
       return m[1] + 1;
    }
    
22.10.2016 / 02:25