mardi 1 décembre 2015

How to get both Random Date and Time C#

I'm trying to write a code the generate random date and time where all values of (yyyy-mm-dd hh:mm:ss) are changed

I used this code but it change only the date and fix 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 ok if I write a code that generate a random date, and store it in a variable1, then write another code that generate random time, and store it in variable2, then pick the date only from variable1, and pick the time only from variable2, then combine variable1 and variable2 together , so I can generate random date and time therefore, I wrote this another method to generate the 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 in the RandomTime method, it generate Random hours only, doesn't change the values of the minutes and seconds,

is there any suggestion to solve the code problem? is there anyway to generate random values for all (yyyy-mm-dd hh:mm:ss) ?




Aucun commentaire:

Enregistrer un commentaire