2

I am trying to generate a random time between 12AM and 6PM. So far I tried the following code:

Random random = new Random();

TimeSpan startWorkDay = new TimeSpan(5, 0, 0);
TimeSpan endWorkDay = new TimeSpan(12, 0, 0);

TimeSpan numberOfMinutes = endWorkDay - startWorkDay;
TimeSpan timeSpan = new TimeSpan(0, random.Next(0, (int)numberOfMinutes.TotalMinutes), 0);

DateTime flightTimeSpan = startWorkDay + timeSpan;

for (int i = 0; i < 10; i++)
{
    Console.WriteLine(flightTimeSpan.ToString("hh:mm tt"));
}

what am I doing wrong?

EDIT: The code provided above is saying that I cannot implicitly convert System.TimeSpan to System.DateTime

  • What output and/or error does this produce? What do you expect the output to be? – Herohtar Nov 03 '19 at 04:32
  • I am trying to get a time in a hh:mm tt format, the code I provided arguing, that I cannot convert a TimeSpan to a DateTime – Seid Akhmed Agitaev Nov 03 '19 at 04:35
  • 1
    https://stackoverflow.com/questions/1483670/whats-the-best-practice-for-getting-a-random-datetime-between-two-date-times – fuzzybear Nov 03 '19 at 04:36
  • `DateTime flightTimeSpan = startWorkDay + timeSpan;` <-- You are adding two `TimeSpan` objects, which results in another `TimeSpan`, but you're trying to assign it to a `DateTime` object, which is not valid. – Herohtar Nov 03 '19 at 04:37
  • how can I possibly convert it to a DateTime? – Seid Akhmed Agitaev Nov 03 '19 at 04:38
  • You should probably be using `DateTime` for your `startWorkDay` and `endWorkDay` values. A `TimeSpan` represents a duration, but you're likely wanting to refer to a specific point in time. Take a look at the answer in the question that @saj linked. – Herohtar Nov 03 '19 at 04:40
  • `var result = DateTime.MinValue + yourTimeSpan` ought to do it. – Robert Harvey Nov 03 '19 at 04:40
  • saj, I tried that one as well, but still I am getting an error that I cannot convert TimeSpan to DateTime – Seid Akhmed Agitaev Nov 03 '19 at 04:41
  • When you declare a Timespan of TimeSpan(12, 0, 0) that isn't a time, it's just 12 a representation of 12 hours. So when you add two Timespan objects and you want a DateTime out of it then c# can't do it because it can't answer the question "12 hours from when". But you can do DateTime + TimeSpan and get a DateTime. So you could define startWorkDay and endWorkdDay as DateTimes instead of Timespans, which wouwld also make more sense. – danielm Nov 03 '19 at 04:42

2 Answers2

4

Here is a way to achieve the goal from the first line of the question.

I am trying to generate a random time between 12AM and 6PM.

var rnd = new Random(i);//Fixed seed, just termporarily
var minutes = rnd.Next(0, 18 * 60);
var timeOfDay = TimeSpan.FromMinutes(minutes);

Test

var rnd = new Random(i);//Fixed seed, just as an example
for(int i = 0; i < 10; i++)
{
    var minutes = rnd.Next(0, 18 * 60);

    var timeOfDay = TimeSpan.FromMinutes(minutes);

    var dt = new DateTime(2019, 11, 03) + timeOfDay;

    Console.WriteLine(dt.ToString("hh:mm tt"));
}
// .NETCoreApp,Version=v3.0
01:04 PM
04:28 AM
01:52 PM
05:17 AM
02:41 PM
06:05 AM
03:29 PM
06:53 AM
04:18 PM
07:42 AM
tymtam
  • 31,798
  • 8
  • 86
  • 126
  • Would be better to create the `Random` instance outside the loop, and use the parameterless constructor (which uses a time-dependent seed). – Matt Johnson-Pint Nov 03 '19 at 18:46
1

You have to convert TimeSpan to DateTime before using ToString("hh:mm tt")

TimeSpan flightTimeSpan = startWorkDay + timeSpan;
DateTime flightDateTime = new DateTime(flightTimeSpan.Ticks);

for (int i = 0; i < 10; i++)
{
    Console.WriteLine(flightDateTime.ToString("hh:mm tt"));
}
Ray Krungkaew
  • 6,652
  • 1
  • 17
  • 28