首页 > 编程 > C# > 正文

c# 生成随机时间的小例子

2020-01-24 03:10:42
字体:
来源:转载
供稿:网友
复制代码 代码如下:

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();
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表