Generate random date for test data with JavaScript

Siddharth Murugan
4 min readApr 2, 2024

--

While testing a signup flow or a user data filling page, you may need to generate random Date of Birth to fill in the field. Though we have many packages available to generate random date, let us create our own function to generate date.

Math.random()

Before getting started with date generation function, let us try to understand about Math.random() function. This is an inbuilt function of JavaScript, which return random floating point number greater than 0 but less than 1.

Math.floor()

This is also an inbuilt function of JavaScript, which round off the number to less than the given integer value.

For example,

Math.floor(2.7) 
//Gives 2 as output

Generating random number within specific range

Now, let us generate a random number between a specific range. For example, let us generate a random number ranging between 1 to 12. For this you should follow below formula,

let StartingRange = 1
//Note that ending range isn't inclusive so we need to give next number
let EndingRange = 13
Math.random() * (EndingRange - StartingRange) + StartingRange

Note that the ending number range isn’t inclusive, so we need to give the next number.

In our case, we need to give the ending range as 13 to include 12 in our random number generation range.

We are now done with the random month generation. Yes! the month ranges between 1 to 12 and we have successfully generated it.

Handing 31 days in a month

Next step is to generate date ranging between 1 to 30/31. For that, let us identify the month which has 30 and 31 days. We will store the month which ever has 31 days in a variable and check whether our random month generated value includes in it. Also let us generate year with specific range from 1991–2005.

//Month ranges between 1 to 12 months
let month = Math.floor(Math.random()*(13-1)+1)
//Identifying months which has 31 days
let oddDays = [1,3,5,7,8,10,12]
//Picking random year between 1901 to 2005
let year = Math.floor(Math.random()*(2005-1900)+1900)
//Checking whether random month we generated has 31 days
let day31 = oddDays.includes(month)

Script to generate 31 date goes below,

if(day31)
{
//Generating date between 1 to 31 days
let date = Math.floor(Math.random()*(32-1)+1)
return new Date(year+'-'+month+'-'+date)
}

Handling leap year and February month

In the else part of the script, let us handle leap year. For that, we need to verify whether the given year is divisible by 4 and then the month is 2.

Also we need to define the date range for other months between 1 to 30.

else{
//Checking whether the given year is a leap year and the month is february
if(year%4 == 0 && month == 2)
{
//Since its a leap year we should have date ranging between 1 to 29
let date = Math.floor(Math.random()*(30-1)+1)
return new Date(year+'-'+month+'-'+date)
}
//checking whether the given year is not a leap year and the month is february
else if(year%4 != 0 && month == 2)
{
//Since month is february, we are generating date ranging between 1 to 28
let date = Math.floor(Math.random()*(29-1)+1)
return new Date(year+'-'+month+'-'+date)
}
else{
//Since it is not a leap year and the month is not february, we are generating date between 1 to 30
let date = Math.floor(Math.random()*(31-1)+1)
return new Date(year+'-'+month+'-'+date)
}
}

Combined code

So the combined code will look like below. This could store the date and return it in the form of date.


function randomDate()
{
//Month ranges between 1 to 12 months
let month = Math.floor(Math.random()*(13-1)+1)
//Identifying months which has 31 days
let oddDays = [1,3,5,7,8,10,12]
//Picking random year between 1901 to 2005
let year = Math.floor(Math.random()*(2005-1900)+1900)
//Checking whether random month we generated has 31 days
let day31 = oddDays.includes(month)
//Handling code if the month has 31 days
if(day31)
{
//Generating date between 1 to 31 days
let date = Math.floor(Math.random()*(32-1)+1)
return new Date(year+'-'+month+'-'+date)
}
else{
//Checking whether the given year is a leap year and the month is february
if(year%4 == 0 && month == 2)
{
//Since its a leap year we should have date ranging between 1 to 29
let date = Math.floor(Math.random()*(30-1)+1)
return new Date(year+'-'+month+'-'+date)
}
//checking whether the given year is not a leap year and the month is february
else if(year%4 != 0 && month == 2)
{
//Since month is february, we are generating date ranging between 1 to 28
let date = Math.floor(Math.random()*(29-1)+1)
return new Date(year+'-'+month+'-'+date)
}
else{
//Since it is not a leap year and the month is not february, we are generating date between 1 to 30
let date = Math.floor(Math.random()*(31-1)+1)
return new Date(year+'-'+month+'-'+date)
}
}
}

let dob = randomDate()
console.log("Here is your dob:"+dob)

Hurray! you are now done with the date generation! We will see more on how to generate date of birth with specific age in next article. Happy coding!

--

--

Siddharth Murugan

Programmer who loves to do things creatively. #automationTester by profession #javascript #nodejs #reactjs