I am using protractor 5.2.2. We have a requirement of creating a module with unique date so that i cannot create a module with already used date.So when i am running the script , i have to pass different date each time.How we can choose random date in automation.Thanks in advance.
Asked
Active
Viewed 127 times
3 Answers
1
I recommend using chancejs.
var Chance = require('chance'),
chance = new Chance();
console.log(chance.integer({ min: -2, max: 2 }));
would return either -2, -1, 0, 1, or 2.
Please take a look at the chancejs homepage http://chancejs.com/
Below example gives a data between these two years
var Chance = require('chance');
var chance = new Chance();
let bounds = {
min: chance.date({ year: 1983 }),
max: chance.date({ year: 1989 })
}
let date = chance.date(bounds)
console.log(date);
I got 1987-01-21T19:31:32.851Z

Bharath Kumar S
- 1,410
- 2
- 10
- 29
-
Thanks for your reply.Actually i need to generate a future date of format "MM-DD-YYYY" and pass that date into a date field. i have tried with below code by passing a start&end date to a function.but sometimes it gives same date even though it is random. moment(new Date(start.getTime() + (Math.floor(Math.random()*3 ) + 1 ) * (end.getTime() - start.getTime()))).format('MM-DD-YYYY'); Can we do this using chancejs? – Devleena Aug 14 '18 at 06:52
-
Edited the answer with date in chancejs – Bharath Kumar S Aug 14 '18 at 07:01
-
Great..Can we pass a min&max year to this, to generate a random date in between that 2 years. – Devleena Aug 14 '18 at 07:33
-
mention the maximum and minimum year – Bharath Kumar S Aug 14 '18 at 09:01
0
1.Random dates in JAVA Generate random date of birth
if you are using the Excel as data provider Use the Excel formula's like
=today(); =now();
if you are using java or other languages
use
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyyHH:mm:ss"); Date date = new Date();
C# https://stackoverflow.com/questions/6817266/get-current-date-only-in-c-sharp

Error Hunter
- 1,354
- 3
- 13
- 35
0
MomentJS is a good option that allows you to set time easily off the current time.
Examples:
moment.format('MM/DD/YYYY'); //gives you current date in mm/dd/yyyy format
moment.format('MM-DD-YYYY'); //current date in mm-dd-yyyy format
moment.add('5','days').format('MM/DD/YYYY'); gives you date 5 days from now

Ben Mohorc
- 694
- 6
- 16