当前位置: 编程技术>.net/c#/asp.net
c# 生成随机时间的小例子
来源: 互联网 发布时间:2014-10-22
本文导语: 代码如下:Random random = new Random((int)(DateTime.Now.Ticks)); int i = 0; while (i < 100) { int hour = random.Next(2, 5); int minute = random.Next(0, 60); ...
代码如下:
Random random = new Random((int)(DateTime.Now.Ticks));
int i = 0;
while (i < 100)
{
int hour = random.Next(2, 5);
int minute = random.Next(0, 60);
int second = random.Next(0, 60);
string tempStr = string.Format("{0} {1}:{2}:{3}", DateTime.Now.ToString("yyyy-MM-dd"), hour, minute, second);
DateTime rTime = Convert.ToDateTime(tempStr);
Console.WriteLine(rTime.ToString());
i++;
}
Console.ReadKey();