Answered

Need to generate two random dates in week day

The challenge is creating two random numbers with difference of two days and random dates generated should be within the week days i.e. these days should not cover weekend days.

Ex : 

Correct Example :-  Random dates = 8/10/2020 to 8/12/2020 covers only week days

Wrong Example :-   Random dates = 8/7/2020 to 8/9/2020 covers weekend days

0
1 comment

Official comment

Avatar
Payam Fard

Please try the following script. Starting_date will be the first Monday following the current date and ending_date is 2 days after that. Credit for the algorithm to find the closest Monday goes to the website: https://stackoverflow.com/questions/33078406/getting-the-date-of-next-monday

WEB VALUE_GET RESULT_VARIABLE date SOURCE JAVA_SCRIPT var curr_date = new Date(); // current date

var day_info = 8.64e+7; // milliseconds per day
var days_to_monday = 8 - curr_date.getDay(); // days left to closest Monday
var monday_in_sec = curr_date.getTime() + days_to_monday * day_info; // aleary Monday in seconds from 1970
var next_monday = new Date(monday_in_sec); // Monday in date object
next_monday.setHours(0,0,0);
return next_monday; EXCLUDECHARS HALT ON ERROR
VALUE_SET_DATE @date RESULT_VARIABLE starting_date OFFSET ESCAPE HALT ON ERROR
VALUE_SET_DATE @starting_date RESULT_VARIABLE ending_date OFFSET +2d ESCAPE HALT ON ERROR

Comment actions Permalink

Please sign in to leave a comment.