Generates random date from minimal year to today. Default minimal date is 1990.1.1 - CSharp System

CSharp examples for System:Random

Description

Generates random date from minimal year to today. Default minimal date is 1990.1.1

Demo Code


using System.Collections.Generic;
using System;//  ww w  .j  a va  2s .co  m

public class Main{
        /// <summary>
        /// Generates random date from minimal year to today. Default minimal date is 1990.1.1
        /// </summary>
        public static DateTime GetRandomDate(int minimalYear = 1990)
        {
            DateTime startDate = new DateTime(minimalYear, 1, 1);
            int range = (DateTime.Today - startDate).Days;
            var generatedDate = startDate.AddDays(SystemRandom.Next(range));

            return generatedDate;
        }
}

Related Tutorials