How to set a specific future date in a variable eliminating Saturday and Sunday?

josenava
Tera Expert

Hello everyone,

I am building a catalog in which one of the variables is "Date" what I need is that when the user opens the form, automatically the date will be set for example 3 days from now, but weekends don“t have to count as counting days.

For example if the ticket is opened on a Friday, the ticket will be due ( for 3 days) Wednesday of the following week.

In this image, I created the ticket today, 2019-03-27 and the ticket will be due 2019-03-31, but it is a Sunday, could anyone guide me? 

find_real_file.png

Thank you

1 ACCEPTED SOLUTION

It sounds like all you need to do is determine what day of the week today is and then add the appropriate padding to exclude the weekends. Taking your above script, I reformatted it slightly:

function onLoad() {
    var today = new Date();
    var dOw = today.getDay(); //Determine day Of week
    var addDays = 3; //Default number of days to add
    if (dOw == 3 || dOw == 4 || dOw == 5) { //Is Wednesday, Thursday, Friday
        addDays = 5;
    }
    var Days = new Date();
    Days.setDate(today.getDate() + addDays);
    var DaysFormatted = Days.getFullYear() + '-' + ('0' + (Days.getMonth() + 1)).slice(-2) + '-' + ('0' + (Days.getDate() + 0)).slice(-2);
    g_form.setValue('date',DaysFormatted );
}

View solution in original post

9 REPLIES 9

AirSquire
Tera Guru

Try running the below code in background script

var dt = new GlideDateTime()
var gdt = new GlideDateTime(dt);//Thursday
gs.info(gdt.getDayOfWeekLocalTime());

Use GlideAjax for this.

You can use below method to get the day of the week. If it it is 6 add 2 days to the current date and if it is 7 add 1 day and return the value from a script Include.

getDayOfWeekLocalTime()

Regards
Air

josenava
Tera Expert

Hello everyone, 

I got this script, it sets the time to the number of days you need but I would like to eliminate weekends, I mean not to count those days, this is what I have:

-----------------------------------------------------------

function onLoad() {


//Type appropriate comment here, and begin script below


var today = new Date();

 var Days = new Date();

 Days.setDate(today.getDate() + 3);

 var DaysFormatted = Days.getFullYear() + '-' + ('0' + (Days.getMonth() + 1)).slice(-2) + '-' + ('0' + (Days.getDate() + 0)).slice(-2);

 console.log(DaysFormatted);

 g_form.setValue('date',DaysFormatted );

}

-------------------------------------------------------------------------

Can anyone give me a push into the right direction?

Thanks!

It sounds like all you need to do is determine what day of the week today is and then add the appropriate padding to exclude the weekends. Taking your above script, I reformatted it slightly:

function onLoad() {
    var today = new Date();
    var dOw = today.getDay(); //Determine day Of week
    var addDays = 3; //Default number of days to add
    if (dOw == 3 || dOw == 4 || dOw == 5) { //Is Wednesday, Thursday, Friday
        addDays = 5;
    }
    var Days = new Date();
    Days.setDate(today.getDate() + addDays);
    var DaysFormatted = Days.getFullYear() + '-' + ('0' + (Days.getMonth() + 1)).slice(-2) + '-' + ('0' + (Days.getDate() + 0)).slice(-2);
    g_form.setValue('date',DaysFormatted );
}

Thank you for your great help! I have a question though, if I want to add the rest of the days, which are Monday and Tuesday, would the script be like this?

if (dOw == 1 || dOw == 2 || dOw == 3 || dOw == 4 || dOw == 5) { //Monday,Tuesday, Wednesday, Thursday, Friday
addDays = 5;

 

Thank you