-2

Hi How Can I get a random date with hours and minutes in a range bewtween now and 2 or 3 days ago. ??

Thanks to all

something like this dates time with minutes

10/23/2018 4:32:00 PM

10/23/2018 5:31:00 PM

10/23/2018 1:32:00 AM

10/22/2018 2:00:00 PM

Here I can get the dates in a range, let say 2 days, but the hour is the same

public static DateTime NextDateTime(int endDatenumbers)
{
    DateTime startDate = DateTime.Today;

    DateTime endDate = startDate.AddDays(-endDatenumbers);

    var newDate = startDate.AddHours(new Random(Convert.ToInt32(DateTime.Now.Ticks / int.MaxValue)).Next(0, (int)(endDate - startDate).TotalHours));

    return newDate;
}
maccettura
  • 10,514
  • 3
  • 28
  • 35
Kan
  • 111
  • 1
  • 10
  • 2
    What have you tried? Please beware that StackOverflow isn't a code writing service, you need to show your current attempt and what you need help with. – Johan Oct 23 '18 at 20:06
  • 1
    What have you tried? How is the behavior of your code different from the expected behavior? Can you show us your code? Please see the [how to ask a good question](https://stackoverflow.com/help/how-to-ask) page. – Lews Therin Oct 23 '18 at 20:07
  • 2
    https://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number - keep an instance of `Random`, don't create a new one each time – steve16351 Oct 23 '18 at 20:20
  • 1
    This problem is harder than you might think. Suppose the day in question is within two days of the day where daylight savings time changes. One of those days is 23 hours long, and one of them is 25 hours long, so if you go back up to 48 hours, you might be going back *three* days. – Eric Lippert Oct 23 '18 at 20:56

2 Answers2

2

You should simplify that to 1 random call. Get the furthest day which is 3 days ago.

var furthestDate= DateTime.Today.AddDays(-3);

You range is actually 2 days after that date which is (48hrs * 60 min) = 2880 minutes. So anything from that date and 2880 minutes after is valid. Simply get 1 random number between 0 an 2880. Finally simply add the minutes to the furthest date.

var randomDate = furthestDate.AddMinutes(YouRandomNumber);
Franck
  • 4,438
  • 1
  • 28
  • 55
1

The following logic actually computes the number of minutes between the two days. This is important where your days can potentially cross a daylight savings boundary. Also, I am storing the value "today" as technically (albeit unlikely) it could change between the two calls.

    private static DateTime PickRandomMinute(int inPastNDays, Random random)
    {
        DateTime today = DateTime.Today;
        int totalMinutes = (int)(today - today.AddDays(-inPastNDays)).TotalMinutes;
        return today.AddDays(-inPastNDays).AddMinutes(random.Next(totalMinutes));
    }

Example usage:

        Random random = new Random();
        Console.WriteLine(PickRandomMinute(2, random)); // 22/10/2018 9:34:00 PM (for example)
        Console.WriteLine(PickRandomMinute(2, random)); // 23/10/2018 4:55:00 AM (for example)

You don't want to create a new Random within this method because calls that happen very close together would probably end up with the same seed and therefore return the same time.

Adam G
  • 1,283
  • 1
  • 6
  • 15