This article describes the implementation of ASP.net generation of random time in two date ranges. Share to everyone for your reference. The specific methods are analyzed as follows:
Want to find a way to generate random days on the internet can not find, the back had to write their own, affixed to the convenience of everyone to use
train of thought: calculate two date difference days, then in 0 to the difference in the range of days to generate random number, and then the end of the number of days in the part minus this random number, code:
Copy Code code as follows:
<summary>
Generate a random date (within two time ranges)
</summary>
<param name= "Startime" > Start time </param>
<param name= "Endtime" > End time </param>
<returns> return random dates, such as (2014-12-25 00:00:00) </returns>
public static datetime randomtime (DateTime startime, datetime endtime)
{
Random rd = new Random ();
TimeSpan tsp = endtime-startime;
int days = Rd. Next (0,TSP. Days);
DateTime newtime = Endtime. AddDays (-days). AddHours (0). AddMinutes (0). AddSeconds (0);
return newtime;
}
I hope this article will help you with the ASP.net program design.