ASP. NET generates two implementation methods of random time in the date range, asp.net date
This example describes how ASP. NET generates a random time in two date ranges. Share it with you for your reference. The specific method is analyzed as follows:
If you cannot find a method to generate a random number of days on the internet, you have to write it by yourself and paste it for your convenience.
Ideas:Calculate the number of days of the difference between two dates, and generate a random number within the range of 0 to the number of days of the difference, and then subtract the random number from the number of days of the end time. Code:
Copy codeThe Code is as follows: // <summary>
/// (Within two time ranges) generate a random date
/// </Summary>
/// <Param name = "startime"> Start time </param>
/// <Param name = "endtime"> end time </param>
/// <Returns> return a random date, for example (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 design your asp.net program.