Return the first working day of the month

Asked

Viewed 156 times

0

I have the following script and would like it to run only on the first working day of each month, as I can do?


var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('short_description', 'Rotina de Manutenção Preventiva');
    ritm.addQuery('state', '!=', 3).addCondition('state','!=',6).addCondition('state','!=',8);
    ritm.query();

    if(!ritm.next()){
        ritm.initialize(); 
        ritm.description = 'Solicitação aberta para a Realização da Manutenção Preventiva';
        ritm.short_description = 'Rotina de Manutenção Preventiva';
        ritm.u_qs_requested_for = '6816f79cc0a8016401c5a33be04be441';
        ritm.opened_by = '6816f79cc0a8016401c5a33be04be441';

        ritm.insert(); //insert record
    }else{
        ritm.u_fora_do_prazo=true;
        ritm.update();
    }

  • Just to confirm, are you sure this is Javascript?

  • 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

  • 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.

  • 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

  • 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".

3 answers

1

The getDay() method of the Date object returns a number between 0 and 6, being 0 Sunday and 6 Saturday.

So Voce can test whether it’s Saturday or Sunday.

Already know if it’s a holiday or not, just keeping a table even.

0


You can use the method Date() to be able to return a full date and then use the method getDay() to be able to take only the day of the week:

Part 1

let data = new Date();
let dia = data.getDay();

Then optionally you can store every day of the week as String in a Array and then make a comparison if the dataformatada (containing all days of the week) [dia] (which contains the current week day) is equal to the string Segunda then execute whatever is inside the command if:

Part 2

let dataformatada = ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", 
"Sabádo"];

if (dataformatada[dia] == "Segunda") {
    // Aqui vai o código a ser executado!
}

Part 3

        let data = new Date();
        let dia = data.getDay();

        let dataformatada = ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sabádo"];

        if (dataformatada[dia] == "Segunda") {
            var ritm = new GlideRecord('sc_req_item');
            ritm.addQuery('short_description', 'Rotina de Manutenção Preventiva');
            ritm.addQuery('state', '!=', 3).addCondition('state','!=',6).addCondition('state','!=',8);
            ritm.query();

            if(!ritm.next()){
                ritm.initialize(); 
                ritm.description = 'Solicitação aberta para a Realização da Manutenção Preventiva';
                ritm.short_description = 'Rotina de Manutenção Preventiva';
                ritm.u_qs_requested_for = '6816f79cc0a8016401c5a33be04be441';
                ritm.opened_by = '6816f79cc0a8016401c5a33be04be441';

                ritm.insert(); //insert record
            }else{
                ritm.u_fora_do_prazo=true;
                ritm.update();
            }
        }

  • I don’t understand the if (dataformatada[dia] == "Segunda") {... not always the first working day of the month is a Monday.

  • Oops! I forgot to add some things, I will edit my answer again.

0

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
}

  • Show, it worked. Thanks for the support

  • Blz. If you have solved it is necessary to mark in the answer.

Browser other questions tagged

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