我找不到一个干净的解决方案来随机查找两个日期之间的日期,例如:
var date1 = DateTime.Now;
var date2 = DateTime.Now.AddDays(20);
var randomDateBetween = // any date between date1 and date2
有什么建议可以解决这个问题吗?
答案 0 :(得分:0)
您可以执行以下操作:
var date1 = DateTime.Now;
var date2 = DateTime.Now.AddDays(20);
var days = (date2.Date - date1.Date).Days; // number of days between
Random rand = new Random();
int randDays;
DateTime randomDate;
if (days < 0)
{
randDays = rand.Next(1, Math.Abs(days) - 1);
randomDate = date2.AddDays(randDays);
}
else
{
randDays = rand.Next(1, days - 1);
randomDate = date1.AddDays(randDays);
}
这应该处理date2 < date1
时的情况。
答案 1 :(得分:-1)
<Location />
AuthType openid-connect
Require valid-user
...
</Location>