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

No, 3 days from Monday is Thursday, and 3 Days from Tuesday is Friday, so the default value of 3 still applies. The other days have the appropriate padding so that they fall 3 business days (which exclude weekends) in them.

So, if you think about it, here is the Day mapping:
Monday    + 3 = Thursday
Tuesday   + 3 = Friday
Wednesday + 5 = Monday
Thursday  + 5 = Tuesday
Friday    + 5 = Wednesday

I see, thank you I have another question, as I am creating different scripts, for specific days, this one the one you helped me is for 3 days, I need to create couple more, 5 days and 7 days, in this case should I just change this?

 var addDays = 3; //Default number of days to add

I just change instead of a "3" I can use "5" or "7", and the rest of the script remains the same?

Thank you and sorry for so many questions.

Regards

For 5 days I have changed the if condition and calculated the two extra days to your default value:

function onLoad() {
    var today = new Date();
    var dOw = today.getDay(); //Determine day Of week
    var addDays = 5; //Default number of days to add
    if (dOw != 0 && dOw != 6) { //Is a Weekday
        addDays = addDays += 2; //Adds the weekends to the logic
    }
    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 );
}

For 7 days, the dOw logic is not necessary since it will just be adding the same day a week ahead.

function onLoad() {
    var today = new Date();
    var dOw = today.getDay(); //Determine day Of week
    var addDays = 7; //Default number of days to add
    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 );
}

Thanks a lot!!

Best Regards