How to check if a date is a working day?

Asked

Viewed 1,219 times

-3

How to check if a date is a working day ? Below is my sample code:

void main() {

   var now = new DateTime.now();
   var berlinWallFell = new DateTime.utc(1989, 11, 9);
   var moonLanding = DateTime.parse("1969-07-20 20:18:04Z");  // 8:18pm

   print(now);

   // Colocar aqui uma verificação se é dia útil.

}  
  • What have you tried to do? Just compare if the day is not Friday, Saturday or holiday, depending on the logic of course.

  • Do you say days other than Saturday and Sunday? Or holidays in general?

  • Take a look at this question Dart Check if a date is valid, even if it doesn’t suit your case, you’ll understand a little more about!

1 answer

3

In general, working days are those that are not Saturday, Sunday or holiday.

Taking that as a basis a way of doing it would be:

void main() {
  // Data que está validando se é dia util.
  var data = DateTime(2020,02,25);  

  var diaUtil = !ehSabadoOuDomingo(data) && !ehFeriado(data);

  print(diaUtil);
}

bool ehSabadoOuDomingo(DateTime data){
  var diaDaSemana = data.weekday;

  // O "weekday" de uma instancia de data retorna um
  // inteiro referente ao dia da semana. Sábado e domingo
  // são representados respectivamente por 6 e 7.
  return diaDaSemana == 6 || diaDaSemana == 7;
}

bool ehFeriado(DateTime data){

  // Informar a lista de dias no ano que são considerados
  // feriados nacionais. Vai ser necessário pesquisar quais são
  // e ir inserindo aqui nesta lista. Coloquei apenas alguns exemplos.
  var diasDeFeriado = [
    DateTime(2020,01,01),
    DateTime(2020,02,25),
    DateTime(2020,04,21),
  ];

  return diasDeFeriado.contains(data);
}

This is just an example. It can be copied and pasted into Dartpad to test.

  • It would be interesting to refer to Dartpad you put a link to redirect to it, many people do not know what it is.

  • 1

    Good tip, I edited the answer adding the link.

Browser other questions tagged

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