1 /// <summary> 2 /// 返回一个时间 3 /// </summary> 4 /// <param name="url"></param> 5 /// <returns></returns> 6 public DateTime returnTime()//返回随机时间 7 { 8 DateTime dt_now = DateTime.Now; 9 DateTime dt_page = returnPageTime();//其他地方获取到的时间 10 11 DateTime dateTimeMin = Convert.ToDateTime(dt_page); 12 DateTime dateTimeMax = Convert.ToDateTime(dt_now); 13 dateTimeMax = dateTimeMax.AddDays(3); 14 15 TimeSpan ts = dateTimeMax - dateTimeMin; 16 DateTime rTime = DateTime.Now; 17 do 18 { 19 Random r = new Random(); 20 //int t1 = r.Next(0, (int)ts.TotalDays); 21 int t2 = r.Next(0, (int)ts.TotalHours); 22 //int t3 = r.Next(0, (int)ts.TotalMinutes); 23 //int t4 = r.Next(0, (int)ts.TotalDays); 24 25 DateTime newDT = dateTimeMin.Add(new TimeSpan(0, t2, 0, 0)); 26 27 Random random = new Random((int)(DateTime.Now.Ticks)); 28 29 int hour = random.Next(7, 22); 30 int minute = random.Next(0, 60); 31 int second = random.Next(0, 60); 32 string tempStr = string.Format("{0} {1}:{2}:{3}", newDT.ToString("yyyy-MM-dd"), hour, minute, second); 33 rTime = Convert.ToDateTime(tempStr); 34 } 35 while (rTime >= dateTimeMax || rTime <= dateTimeMin); 36 37 38 return rTime; 39 }