ASP. NET generates two implementation methods of random time in the date range, asp.net date

Source: Internet
Author: User
Tags ranges

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.