Asp. NET generation of random time in two date range _ Practical skills

Source: Internet
Author: User

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.

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.