locked
Generate random date and add 10 days RRS feed

  • Question

  • User539757411 posted

    I want to generate a random date and another date 10 days before that date.

    Lets say random generates a startdate 2020-05-01, so it would generate a enddate of 2020-05-11 . 
    The date couldn' be bigger than today date.

    Thanks in advance

    Friday, June 26, 2020 10:54 PM

All replies

  • User-943250815 posted

    Just one idea. Using Ticks.
    Ticks are int64, so for random have to use NextDouble, it works, but can take a long time due to generated random outside range
    https://docs.microsoft.com/en-us/dotnet/api/system.random?view=netcore-3.1

    Protected Function RndDate() As DateTime
        Dim min As Long = New DateTime(1900, 1, 1).Ticks
        Dim max As Long = Now.Ticks
        Dim rand As New Random
        Dim randomDate As Long = rand.NextDouble * Int64.MaxValue
        Dim ok As Boolean = False
        While ok = False
          If randomDate < min Or randomDate > max Then
            randomDate = rand.NextDouble * Int64.MaxValue
          Else
            ok = True
          End If
        End While
        Dim NewDate As DateTime = New DateTime(randomDate)
        Return NewDate.AddDays(10)
      End Function

    Friday, June 26, 2020 11:57 PM
  • User539757411 posted

    Just one idea. Using Ticks.
    Ticks are int64, so for random have to use NextDouble, it works, but can take a long time due to generated random outside range
    https://docs.microsoft.com/en-us/dotnet/api/system.random?view=netcore-3.1

    Protected Function RndDate() As DateTime
        Dim min As Long = New DateTime(1900, 1, 1).Ticks
        Dim max As Long = Now.Ticks
        Dim rand As New Random
        Dim randomDate As Long = rand.NextDouble * Int64.MaxValue
        Dim ok As Boolean = False
        While ok = False
          If randomDate < min Or randomDate > max Then
            randomDate = rand.NextDouble * Int64.MaxValue
          Else
            ok = True
          End If
        End While
        Dim NewDate As DateTime = New DateTime(randomDate)
        Return NewDate.AddDays(10)
      End Function

    Sorry. I meant in Javascript.

    Thanks!

    Saturday, June 27, 2020 12:18 AM
  • User-2054057000 posted

    This is the way to add 10 days to a date in JavaScript:

    var someDate = new Date();
    var numberOfDaysToAdd = 10;
    someDate.setDate(someDate.getDate() + numberOfDaysToAdd); 

    To generate a random date see https://stackoverflow.com/questions/31378526/generate-random-date-between-two-dates-and-times-in-javascript/46093680

    Saturday, June 27, 2020 4:26 AM
  • User1535942433 posted

    Hi xandeq,

    Accroding to your description,as far as I think,you could do like this:

     <script>
            function randomDate(start, end) {
                return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
            }
            var startime = randomDate(new Date(2012, 0, 1), new Date());
            var endtime = new Date(startime.getTime() + (3600 * 1000 * 24*10));
            alert('startime:' + startime +'endtime:' + endtime);
        </script>

    Result:

    Best regards,

    Yijing Sun

    Monday, June 29, 2020 6:50 AM