Receive a date array other than the one chosen

Asked

Viewed 31 times

1

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

I am using this plugin/Jquery component: http://multidatespickr.sourceforge.net/

My problem is that I don’t want to save the dates chosen but the ones not marked.

Very simple example:

pick 10/10/2016

I have to receive in my input text:

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

Code:
http://jsfiddle.net/3t4j9/

2 answers

2


Cycle out to run all the dates of the given month and filter out the ones you don’t want.

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

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: http://jsfiddle.net/s6Lv85wz/

  • Thank you very much @Sergio. That’s what I wanted. Great week everybody!

0

You can check the click of your Ubmit button, in case you exchange for 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 the form Ubmit. Below is a function to check the last day of the month that can help in 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;
    }

Browser other questions tagged

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