Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate random dates within a range? [duplicate]

Tags:

c#

datetime

I am automating an application where records can be opened between specified date ranges. I know that I can create an array that randomly picks a number in my array, such as:

DateTime.Now.AddDays(90).ToString("MM/dd/yyyy")

However, I would like to know if there is a better way to go about this. Please help! Thanks.

like image 725
user2917239 Avatar asked Jan 28 '26 02:01

user2917239


1 Answers

void Main()
{   
    Console.WriteLine(DateTime.UtcNow.AddDays(new Random().Next(90)));
}

This will add a random amount of days to the start date, essentially ending up with a random date.

like image 94
Jeroen Vannevel Avatar answered Jan 29 '26 16:01

Jeroen Vannevel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!