DateTime dateTimeMin = Convert.ToDateTime("2007-1-1");
DateTime dateTimeMax = Convert.ToDateTime("2007-12-31");
我现在想在dateTimeMin 和dateTimeMax 之间取随机值,应该怎么取呢?
请各位XDJM赐教!~谢谢!
10 个解决方案
#1
自己定!~
#2
up
#3
其实你可以随机分别产生年份月份日期啊
#4
产生一个0~365之间的随机数,用dateTimeMin.addDays(随机数)?(好像是这个)。
#5
Random rnd=new Random();
int i=rnd.Next(300);//括号中的值可按实际需要而定
DateTime d=DateTime.Now-new TimeSpan(i);//使用加法或减法依需要而定,也不一定以当前时间为基数
#6
#7
DateTime dateTimeMin = Convert.ToDateTime("2007-1-1");
Random rnd=new Random();
int i=rnd.Next(365);
DateTime dateTime=dateTimeMin.AddDays(i);
#8
DateTime dateTimeMin = Convert.ToDateTime("2007-1-1");
DateTime dateTimeMax = Convert.ToDateTime("2007-12-31");
TimeSpan ts = dateTimeMax - dateTimeMin;
Random r = new Random();
int t1 = r.Next(1,(int)ts.TotalDays);
DateTime newDT = dateTimeMin.Add(new TimeSpan(t1, 0, 0, 0));
DateTime newDT = dateTimeMin + new TimeSpan(t1, 0, 0, 0);//或这种方式
#9
DateTime dateTimeMin = Convert.ToDateTime("2007-1-1");
DateTime dateTimeMax = Convert.ToDateTime("2007-12-31");
TimeSpan ts = new TimeSpan(dateTimeMax.Ticks - dateTimeMin.Ticks);
Random random = new Random();
DateTime dateRandom = dateTimeMin.AddDays(random.Next(ts.Days + 1)); // 得到两个时间相差多少天,然后在其中抽取一天
Console.WriteLine(dateRandom);
#10
public DateTime GetRandomDateTime(ref DateTime startDateTime,ref DateTime endDateTime)
{
randomNumber = new Random(GetRandomSeed(10));
TimeSpan diff = endDateTime - startDateTime;
Double totalSeconds = diff.TotalSeconds;
Double randomSeconds = randomNumber.NextDouble() * totalSeconds;
DateTime randomDateTime = startDateTime.AddSeconds(randomSeconds);
return randomDateTime;
}
{
randomNumber = new Random(GetRandomSeed(10));
TimeSpan diff = endDateTime - startDateTime;
Double totalSeconds = diff.TotalSeconds;
Double randomSeconds = randomNumber.NextDouble() * totalSeconds;
DateTime randomDateTime = startDateTime.AddSeconds(randomSeconds);
return randomDateTime;
}
#1
自己定!~
#2
up
#3
其实你可以随机分别产生年份月份日期啊
#4
产生一个0~365之间的随机数,用dateTimeMin.addDays(随机数)?(好像是这个)。
#5
Random rnd=new Random();
int i=rnd.Next(300);//括号中的值可按实际需要而定
DateTime d=DateTime.Now-new TimeSpan(i);//使用加法或减法依需要而定,也不一定以当前时间为基数
#6
#7
DateTime dateTimeMin = Convert.ToDateTime("2007-1-1");
Random rnd=new Random();
int i=rnd.Next(365);
DateTime dateTime=dateTimeMin.AddDays(i);
#8
DateTime dateTimeMin = Convert.ToDateTime("2007-1-1");
DateTime dateTimeMax = Convert.ToDateTime("2007-12-31");
TimeSpan ts = dateTimeMax - dateTimeMin;
Random r = new Random();
int t1 = r.Next(1,(int)ts.TotalDays);
DateTime newDT = dateTimeMin.Add(new TimeSpan(t1, 0, 0, 0));
DateTime newDT = dateTimeMin + new TimeSpan(t1, 0, 0, 0);//或这种方式
#9
DateTime dateTimeMin = Convert.ToDateTime("2007-1-1");
DateTime dateTimeMax = Convert.ToDateTime("2007-12-31");
TimeSpan ts = new TimeSpan(dateTimeMax.Ticks - dateTimeMin.Ticks);
Random random = new Random();
DateTime dateRandom = dateTimeMin.AddDays(random.Next(ts.Days + 1)); // 得到两个时间相差多少天,然后在其中抽取一天
Console.WriteLine(dateRandom);
#10
public DateTime GetRandomDateTime(ref DateTime startDateTime,ref DateTime endDateTime)
{
randomNumber = new Random(GetRandomSeed(10));
TimeSpan diff = endDateTime - startDateTime;
Double totalSeconds = diff.TotalSeconds;
Double randomSeconds = randomNumber.NextDouble() * totalSeconds;
DateTime randomDateTime = startDateTime.AddSeconds(randomSeconds);
return randomDateTime;
}
{
randomNumber = new Random(GetRandomSeed(10));
TimeSpan diff = endDateTime - startDateTime;
Double totalSeconds = diff.TotalSeconds;
Double randomSeconds = randomNumber.NextDouble() * totalSeconds;
DateTime randomDateTime = startDateTime.AddSeconds(randomSeconds);
return randomDateTime;
}