var startDate = DateTime.Now.AddYears(-100);
var endDate = DateTime.Now;
Random rnd = new Random();
for (int i = 0; i < 10; i++)
{
var randomYear = rnd.Next(startDate.Year, endDate.Year);
var randomMonth = rnd.Next(1, 12);
var randomDay = rnd.Next(1, DateTime.DaysInMonth(randomYear, randomMonth));
if (randomYear == startDate.Year)
{
randomMonth = rnd.Next(startDate.Month, 12);
if (randomMonth == startDate.Month)
randomDay = rnd.Next(startDate.Day, DateTime.DaysInMonth(randomYear, randomMonth));
}
if (randomYear == endDate.Year)
{
randomMonth = rnd.Next(1, endDate.Month);
if (randomMonth == endDate.Month)
randomDay = rnd.Next(1, endDate.Day);
}
var randomDate = new DateTime(randomYear, randomMonth, randomDay);
Console.WriteLine($"Random date: Year {randomDate.Year}, month {randomDate.Month}, day {randomDate.Day}");
}
private static int[] Date(DateTime startDate, DateTime endDate)
{
var rnd = new Random() ;
var randomYear = rnd.Next(startDate.Year, endDate.Year) ;
var randomMonth = rnd.Next(1, 12) ;
var randomDay = rnd.Next(1, DateTime.DaysInMonth(randomYear, randomMonth)) ;
if (randomYear == startDate.Year)
{
randomMonth = rnd.Next(startDate.Month, 12) ;
if (randomMonth == startDate.Month)
randomDay = rnd.Next(startDate.Day, DateTime.DaysInMonth(randomYear, randomMonth)) ;
}
if (randomYear == endDate.Year)
{
randomMonth = rnd.Next(1, endDate.Month) ;
if (randomMonth == endDate.Month)
randomDay = rnd.Next(1, endDate.Day) ;
}
var randomDate = new DateTime(randomYear, randomMonth, randomDay) ;
return new [] { randomDate.Day, randomDate.Month, randomDate.Year } ;
}
var s = Date(new DateTime(1800, 12, 31), new DateTime(2014, 12, 31));
textBox1.Text = s[0].ToString() + "."+ s[1].ToString() + "." + s[2].ToString();