Skip to content

Instantly share code, notes, and snippets.

@sistematico
Created January 20, 2024 11:04
Show Gist options
  • Save sistematico/9bde73f02eccd6c14bf574cf7fe5ea6b to your computer and use it in GitHub Desktop.
Save sistematico/9bde73f02eccd6c14bf574cf7fe5ea6b to your computer and use it in GitHub Desktop.
Determinar o último dia útil não feriado
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);
@sistematico
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment