Random Date - CSharp System

CSharp examples for System:DateTime Calculate

Description

Random Date

Demo Code


using System.Windows.Media;
using System.Text;
using System;/*  ww  w  .jav a 2s .c o m*/

public class Main{
        private static readonly Random RandomSeed = new Random();
        public static DateTime RandomDate(DateTime from, DateTime to) {
            var range = new TimeSpan(to.Ticks - from.Ticks);
            return from + new TimeSpan((long)(range.Ticks * RandomSeed.NextDouble()));
        }
        public static DateTime RandomDate() {
            return RandomDate(new DateTime(1900, 1, 1), DateTime.Now);
        }
}

Related Tutorials