Sunday, 3 March 2013

Generate Random Date using VB.NET

Generate Random Date using  VB.NET

 
 
This example generates random date from 1900 to 9999.   
DaysInMonth will be returned appropriate Days, based on Leap year also.




Private Function GenererateRandomDate() As DateTime

            Dim year As Integer =  rnd.Next(1900,9999)
            Dim month As Integer =  rnd.Next(1,12)
            Dim day As Integer =  DateTime.DaysInMonth(year,month)

            Dim Day As Integer =  rnd.Next(1,day)

            Dim dt As DateTime =  New DateTime(year,month,Day)
            Return dt
 End Function


Construct Random Class object Outside of the function,Make n number of calls.




OUTPUT:
Random Date=10/11/5427 12:00:00 AMRandom Date=6/22/4602 12:00:00 AMRandom Date=9/7/7710 12:00:00 AMRandom Date=10/12/7657 12:00:00 AMRandom Date=8/23/2410 12:00:00 AM
 

Note: User can apply  Standard Date Formats on this.
for ex:  rndDate.ToString("d")   displays short date:  2/18/5335 
other available formats are: o,O,g,G,d,D,s,u,U,f,F,Y,y .


Tags:Generate Random Dates in Vb.NET,  Generate Random Dates in various format,Random Date in Vb.NET.Generate Random Month,Generate random Day,Generate Random Year,How to Generate Random DateTime Object,Generate Random UTC Dates

No comments:

Post a Comment