Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9021563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:17:07+00:00 2026-06-16T05:17:07+00:00

I have StartDate (for instance 2011-01-01) and EndDate (for instance 2011-12-31). All my data

  • 0

I have StartDate (for instance 2011-01-01) and EndDate (for instance 2011-12-31).
All my data are placed in this range.

In test plan I need to generate random interval with fixed duration
(for 4 cases: 1, 3, 7 days and 1 month long)
which are placed in this date range [2011-01-01; 2011-12-31].
Each of these cases must have defined weights.

How should I do to get fixed interval with random Start Date and random End Date (and put these random dates into 2 variables)?

I have found this variant of java-script

The initial script is here:

var startDate = new Date();
startDate.setDate(1);
startDate.setMonth(0);
startDate.setYear(1991);
var startDateTime = startDate.getTime();

var endDate = new Date();
endDate.setDate(31);
endDate.setMonth(11);
endDate.setYear(2003);
var endDateTime = endDate.getTime();

var randomDate = new Date();
var randomDateTime = startDateTime+Math.random()*(endDateTime-startDateTime);
randomDate.setTime(randomDateTime);

var rndDate = randomDate.getDate();
var rndMonth = randomDate.getMonth() + 1;
var rndYear = randomDate.getFullYear();

if (rndDate.toString().length == 1)
rndDate = "0" + rndDate;
if (rndMonth.toString().length == 1)
rndMonth = "0" + rndMonth;

rndDate + "/" + rndMonth + "/" + rndYear;

But I need generate random start date of the fixed interval (which I called ${RandomStartDate}) and then to add the length of the interval to get the end date of it (this date I called ${RandomEndDate}).

Then I have change the script for 1 day long interval (1 day is 86400 seconds):

var startDate = new Date();
startDate.setDate(01);
startDate.setMonth(01);
startDate.setYear(2011);
var startDateTime = startDate.getTime();

var endDate = new Date();
endDate.setDate(31);
endDate.setMonth(12);
endDate.setYear(2011);
var endDateTime = endDate.getTime();

var randomSDate = new Date();
var randomSDateTime = startDateTime+Math.random()*((endDateTime - 86400) -startDateTime );
randomSDate.setTime(randomSDateTime);

var randomEDate = new Date();
var randomEDateTime = (randomSDateTime +  86400);    //add 1 day long interval (86400 s)
randomEDate.setTime(randomEDateTime);   //convert number format to string format of date
var rndSDate = randomSDate.getDate();
var rndSMonth = randomSDate.getMonth()+1 ;
var rndSYear = randomSDate.getFullYear();

var rndEDate = randomEDate.getDate();      
var rndEMonth = randomEDate.getMonth()+1 ;
var rndEYear = randomEDate.getFullYear();

if (rndSDate.toString().length == 1)      
rndSDate = "0" + rndSDate;
if (rndSMonth.toString().length == 1)
rndSMonth = "0" + rndSMonth;

if (rndEDate.toString().length == 1)      
rndEDate = "0" + rndEDate;
if (rndEMonth.toString().length == 1)
rndEMonth = "0" + rndEMonth;

var RandomStartDate = rndSYear + "-" + rndSMonth + "-" + rndSDate;
vars.put ("RandomStartDate", RandomStartDate);

var RandomEndDate = rndEYear + "-" + rndEMonth + "-" + rndEDate;
vars.put ("RandomEndDate", RandomEndDate);

But this script generates RandomEndDate which is equal to RandomStartDate.
If I generate RandomStartDate separately (without the parts of code which are connected with RandomEndDate) the script works good.

Could you help me, please? What is wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:17:08+00:00Added an answer on June 16, 2026 at 5:17 am

    You can do it in Beanshell with much less amount of code:

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    
    calendar = Calendar.getInstance();
    calendar.set(2011, 0, 1);
    startTime = calendar.getTimeInMillis();
    calendar.set(2012, 11, 31);
    endTime = calendar.getTimeInMillis();
    randomTime1 = startTime + (long)(Math.random()*(endTime-startTime));
    randomTime2 = randomTime1 + (long)(Math.random()*(endTime - randomTime1)+86400000);
    
    formatter = new SimpleDateFormat("yyyy-MM-dd");
    calendar.setTimeInMillis(randomTime1);
    vars.put("start", formatter.format(calendar.getTime()));
    calendar.setTimeInMillis(randomTime2);
    vars.put("end", formatter.format(calendar.getTime()));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sample date DateTime startDate = DateTime.Parse(2011-01-01 00:00:00); DateTime endDate = DateTime.Parse(2011-01-03
Say I have a startdate = '2010/04/01' and an enddate = '2011/02/01' . How
I'm have a startDate and a endDate stored in a SQLite database and need
In my MySQL database, I have a startdate and an enddate field. Using today's
I have the following id eventid startdate enddate 1 1 2009-01-03 2009-01-05 1 2
I have two date pickers one for startDate and another one is for endDate.
I have table with two columns (startdate & enddate) and I have two submission
Input @StartDate = '01/25/2010' @EndDate = '02/06/2010' I have 2 CTEs in a stored
I have this <%=Model.StartDate%> <%=Html.Hidden(StartDate, Model.StartDate)%> it outputs: 2010-05-11 11:00:00 +01:00 <input type=hidden value=2010-03-17
I have a report that has parameters StartDate and EndDate . I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.