If it does not matter if the first "working day" of the month is a holiday, then you just want to know if today is neither Saturday nor Sunday, because these two days, whether a holiday or not, are not working days, that is, you want to pick the first day of the month that is Monday to Friday.
You can do this in the form below by comparing the current day with the first day of the month explained above.
First you create an object Date()
to pick up the current date:
var hoje = new Date();
Then another object Date()
to catch the 1st day of the current month:
var pri_dia_util = new Date(hoje.getFullYear(), hoje.getMonth(), 1);
Then you take the day of the week from the above object:
var dia_semana = pri_dia_util.getDay();
Makes a while
until the value dia_semana
is different from 0 (Sunday) or 6 (Saturday). If different, the while
straight pass:
while(dia_semana == 0 || dia_semana == 6){
pri_dia_util.setDate(pri_dia_util.getDate() + 1);
dia_semana = pri_dia_util.getDay();
}
If the first day of the month is Saturday or Sunday, while
increasing the object
pri_dia_util
+1 day until it’s Monday.
And finally you compare the present day with the resulting day of the object pri_dia_util
:
if(hoje == pri_dia_util){
// executar código
}
The whole code will look like this:
var hoje = new Date();
var pri_dia_util = new Date(hoje.getFullYear(), hoje.getMonth(), 1);
var dia_semana = pri_dia_util.getDay();
while(dia_semana == 0 || dia_semana == 6){
pri_dia_util.setDate(pri_dia_util.getDate() + 1);
dia_semana = pri_dia_util.getDay();
}
if(hoje == pri_dia_util){
// executar código
}
If you do a test with the month of September, where day 1 fell on a Sunday, the value of pri_dia_util
will be the day 2 (Monday). See:
var hoje = new Date();
var pri_dia_util = new Date(2019, 8, 1);
var dia_semana = pri_dia_util.getDay();
while(dia_semana == 0 || dia_semana == 6){
pri_dia_util.setDate(pri_dia_util.getDate() + 1);
dia_semana = pri_dia_util.getDay();
}
console.log(pri_dia_util.toLocaleDateString());
if(hoje == pri_dia_util){
// executar código
}
Similarly the month of December/2019, where the day 1 falls on a Sunday, Monday will be the day 2:
var hoje = new Date();
var pri_dia_util = new Date(2019, 11, 1);
var dia_semana = pri_dia_util.getDay();
while(dia_semana == 0 || dia_semana == 6){
pri_dia_util.setDate(pri_dia_util.getDate() + 1);
dia_semana = pri_dia_util.getDay();
}
console.log(pri_dia_util.toLocaleDateString());
if(hoje == pri_dia_util){
// executar código
}
Already in the current month (November/2019) will return the same day 1, as it fell on a Friday:
var hoje = new Date();
var pri_dia_util = new Date(hoje.getFullYear(), hoje.getMonth(), 1);
var dia_semana = pri_dia_util.getDay();
while(dia_semana == 0 || dia_semana == 6){
pri_dia_util.setDate(pri_dia_util.getDate() + 1);
dia_semana = pri_dia_util.getDay();
}
console.log(pri_dia_util.toLocaleDateString());
if(hoje == pri_dia_util){
// executar código
}
Already in June/2019, the first day fell on a Saturday, so Monday was the day 3:
var hoje = new Date();
var pri_dia_util = new Date(2019, 5, 1);
var dia_semana = pri_dia_util.getDay();
while(dia_semana == 0 || dia_semana == 6){
pri_dia_util.setDate(pri_dia_util.getDate() + 1);
dia_semana = pri_dia_util.getDay();
}
console.log(pri_dia_util.toLocaleDateString());
if(hoje == pri_dia_util){
// executar código
}
Just to confirm, are you sure this is Javascript?
– Woss
Yes, I only need to perform this if the day is the first working day. Then I need a check that returns the first working day of the month and if it is the first working day, I run the code above
– Daniel Silva
This is a complicated implementation, can still occur from the second being a holiday. If you want an extra check to make sure it’s the first working day of the month, add another validation if that day isn’t a holiday, with a table of holidays, because in addition to national and world holidays these holidays can be different in each city.
– GabrielLocalhost
I just need to validate working day of the week without knowing if it is a holiday. Only run if the day is the first working day of the month
– Daniel Silva
I do not believe that the JS will be able to tell you when it is or not a holiday, not even in back-end, probably will have to have a "database".
– Guilherme Nascimento