Created
January 20, 2024 11:04
-
-
Save sistematico/9bde73f02eccd6c14bf574cf7fe5ea6b to your computer and use it in GitHub Desktop.
Determinar o último dia útil não feriado
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function ultimaDataUtil(): Date { | |
const path = import.meta.dir + "/../json/feriados.json"; | |
const file = Bun.file(path); | |
const { feriados } = await file.json(); | |
let dataAtual = new Date(); | |
while (true) { | |
const dataFormatada = `${dataAtual.getFullYear()}-${(dataAtual.getMonth() + 1).toString().padStart(2, '0')}-${dataAtual.getDate().toString().padStart(2, '0')}`; | |
const dataFeriado = new Date(dataFormatada); | |
if (dataAtual.getDay() >= 1 && dataAtual.getDay() <= 5 && feriados.filter((feriado: Feriados) => feriado.date === dataFormatada).length === 0) { | |
return new Date(dataFormatada); | |
} | |
dataAtual.setDate(dataAtual.getDate() - 1); | |
} | |
} | |
const diaUtil = await ultimaDataUtil(); | |
console.log(diaUtil); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JSON com os feriados: https://gist.github.com/sistematico/0d795e73e133632204593f1d1db4a618