I am trying to write code to generate random dates and times when all values โโchange (yyyy-mm-dd hh: mm: ss)
I used this code, but it only changes the date and fixes the time
DateTime RandomDay() { DateTime start = new DateTime(2013, 1, 1 , 1 , 1 ,1); Random gen = new Random(); int range = (DateTime.Today - start).Days; return start.AddDays(gen.Next(range)); }
I said, well, if I write a code that generates a random date and save it in variable1, then write another code that generates a random time and store it in variable2, then select the date only from variable1 and select the time only from variable2 and then combine variable1 and variable2 together so that I can generate random date and time so I wrote this other method to generate time
DateTime RandomTime() { DateTime start = new DateTime(2013, 1, 1, 1, 1, 1); Random gen = new Random(); int range = (DateTime.Today - start).Hours; return start.AddHours(gen.Next(range)); }
Now the problem is in the RandomTime method, it only generates random hours, does not change the values โโof minutes and seconds,
Is there any suggestion to solve the problem with the code? is there anyway to generate random values โโfor all (yyyy-mm-dd hh: mm: ss)?
source share