Sujeet Raman

Sujeet Raman

  • 743
  • 915
  • 334.3k

how to get random date in mm/dd format in each iteration in c#

Aug 19 2021 1:09 PM

I have to get a random mm/dd format date(may be 100 counts) in each iteration? for every iteration i need to get different mm/dd with out year,

what i tried here is not working

for(i=0;i<100;i++){

                Random randNum = new Random();

                DateTime minDt = new DateTime(2000, 1, 1, 10, 0, 0);
                DateTime maxDt = new DateTime(2000, 1, 1, 17, 0, 0);
                List<DateTime> myDates = new List<DateTime>();
              
                int minutesDiff = Convert.ToInt32(maxDt.Subtract(minDt).TotalMinutes + 1);

                for (int j = 0; j < 100; j++)
                {
                    // some random number that's no larger than minutesDiff, no smaller than 1
                    int r = randNum.Next(1, minutesDiff);
                    myDates.Add(minDt.AddMinutes(r));
                }
                foreach (DateTime d in myDates)
                {
                    string date = string.Format("{0:dd/MM}", d);
                }

 

}


Answers (9)