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
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
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
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
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Member
251 Points
444 Posts
Generate random date and add 10 days
Jun 26, 2020 10:54 PM|xandeq|LINK
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
Participant
1101 Points
673 Posts
Re: Generate random date and add 10 days
Jun 26, 2020 11:57 PM|jzero|LINK
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
Member
251 Points
444 Posts
Re: Generate random date and add 10 days
Jun 27, 2020 12:18 AM|xandeq|LINK
Sorry. I meant in Javascript.
Thanks!
Participant
1253 Points
943 Posts
Re: Generate random date and add 10 days
Jun 27, 2020 04:26 AM|yogyogi|LINK
This is the way to add 10 days to a date in JavaScript:
To generate a random date see https://stackoverflow.com/questions/31378526/generate-random-date-between-two-dates-and-times-in-javascript/46093680
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Contributor
4060 Points
1587 Posts
Re: Generate random date and add 10 days
Jun 29, 2020 06:50 AM|yij sun|LINK
Hi xandeq,
Accroding to your description,as far as I think,you could do like this:
Result:
Best regards,
Yijing Sun