Do the following.
Retrieve the current date in a variable:
let DATE = new Date(); // Data atual
Retrieve the current month:
let MONTH = DATE.getMonth() + 1; // Mês atual
Remember that the sum of d DATE.getMonth()
plus 1, is due to return method 0 to 11.
Recover the previous month:
let MONTH_PREVIOUS = ( MONTH === 1 ) ? 12 : (MONTH - 1); // Mês anterior
The above condition checks whether the current month is January , if it is, the previous one December .
Recover next month:
let NEXT_MONTH = ( MONTH === 12 ) ? 1 : (MONTH + 1); // Próximo mês
The above condition checks whether the current month is December , if it is, the next January .
Now retrieve the current, previous, and next year:
let YEAR = DATE.getFullYear(); // Ano atual
let YEAR_PREVIOUS = YEAR - 1; // Ano anterior
let NEXT_YEAR = YEAR + 1; // Próximo ano
Declare closing lists:
let LIST_OF_CLOSING = []; // Lista de fechamento
let LIST_THE_NEXT_CLOSINGIN = []; // Lista do próximo fechamento
Next step is to create the method, I will not explain step-by-step, but the code is commented:
// Método para fechamento
const closing = () => {
// Data de fechamento.
let DATE_OF_CLOSING = new Date('${YEAR}-${MONTH}-4').getTime();
// Percorre a lista
LIST.map(item => {
let DATE_ITEM = new Date(item.date).getTime();
// Data de início
let START_DATE = new Date('${( MONTH === 1 ) ? YEAR_PREVIOUS : YEAR}-${MONTH_PREVIOUS}-5').getTime();
// Data do próximo fechamento.
let NEXT_CLOSING_IN = new Date('${( MONTH === 12 ) ? NEXT_YEAR : YEAR}-${NEXT_MONTH}-4').getTime();
/* Verifica se DATE_ITEM é menor ou igual a DATE_OF_CLOSING, depois
* verifica se DATE_ITEM é maior ou igual a START_DATE, se for
* adiciona o item na lista LIST_OF_CLOSING, caso contrário, verifica se
* DATE_ITEM é maior ou igual a DATE_OF_CLOSING, depois verifica se
* DATE_ITEM é menor ou igual a NEXT_CLOSING_IN, se for adiciona o item
* na lista LIST_THE_NEXT_CLOSINGIN, caso contrário não faz nada.
*/
(DATE_ITEM <= DATE_OF_CLOSING && DATE_ITEM >= START_DATE) ? LIST_OF_CLOSING.push(item) : (DATE_ITEM >= DATE_OF_CLOSING && DATE_ITEM <= NEXT_CLOSING_IN) && LIST_THE_NEXT_CLOSINGIN.push(item);
});
}
Example running:
let LIST = [
{ date: '2018-2-4', value: '59.99', type: 'credit', typePag: 1 },
{ date: '2018-2-5', value: '59.99', type: 'debit', typePag: 2 },
{ date: '2018-2-5', value: '59.99', type: 'credit', typePag: 1 },
{ date: '2018-3-5', value: '59.99', type: 'credit', typePag: 1 },
{ date: '2018-3-5', value: '59.99', type: 'debit', typePag: 2 },
{ date: '2018-3-4', value: '59.99', type: 'credit', typePag: 1 },
{ date: '2018-4-5', value: '59.99', type: 'debit', typePag: 2 },
{ date: '2018-4-9', value: '234.99', type: 'credit', typePag: 1 },
{ date: '2018-4-3', value: '89.99', type: 'credit', typePag: 1 },
{ date: '2018-8-5', value: '89.99', type: 'credit', typePag: 1 },
{ date: '2018-4-17', value: '71.08', type: 'debit', typePag: 2 },
{ date: '2018-4-1', value: '151.08', type: 'debit', typePag: 2 },
];
let DATE = new Date();
let MONTH = DATE.getMonth() + 1;
let MONTH_PREVIOUS = ( MONTH === 1 ) ? 12 : (MONTH - 1);
let NEXT_MONTH = ( MONTH === 12 ) ? 1 : (MONTH + 1);
let YEAR = DATE.getFullYear();
let YEAR_PREVIOUS = YEAR - 1;
let NEXT_YEAR = YEAR + 1;
let LIST_OF_CLOSING = [];
let LIST_THE_NEXT_CLOSINGIN = [];
const closing = () => {
let DATE_OF_CLOSING = new Date('${YEAR}-${MONTH}-4').getTime();
LIST.map(item => {
let DATE_ITEM = new Date(item.date).getTime();
let START_DATE = new Date('${( MONTH === 1 ) ? YEAR_PREVIOUS : YEAR}-${MONTH_PREVIOUS}-5').getTime();
let NEXT_CLOSING_IN = new Date('${( MONTH === 12 ) ? NEXT_YEAR : YEAR}-${NEXT_MONTH}-4').getTime();
(DATE_ITEM <= DATE_OF_CLOSING && DATE_ITEM >= START_DATE) ? LIST_OF_CLOSING.push(item) : (DATE_ITEM >= DATE_OF_CLOSING && DATE_ITEM <= NEXT_CLOSING_IN) && LIST_THE_NEXT_CLOSINGIN.push(item);
});
}
closing();
console.log('Lista de fechamento: ', LIST_OF_CLOSING);
console.log('Lista do próximo fechamento: ', LIST_THE_NEXT_CLOSINGIN);