VB.NET - Getting random date that falls between specific M/D/Y range

Posted on 2008-11-16
Last Modified: 2012-05-05
I'm trying to make a function that can return a random DOB between a specific Month, Day & Year range

for example

DOB between 5/25/1981 - 4/14/1985

random result could be: 01/22/84
random result could be: 4/13/85

Question by:mcainc
    1 Comment
    LVL 14

    Accepted Solution

    try this
            Dim dtOne As New DateTime(2008, 1, 1)
            Dim dtTwo As New DateTime(2008, 1, 31)
            Dim ORandomNumber As New Random()
            Dim iTotalDays As Int16
            iTotalDays = DateDiff(DateInterval.Day, dtOne, dtTwo)
            Dim dtRandomDate As DateTime
            dtRandomDate = dtOne.AddDays(ORandomNumber.Next(iTotalDays))

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now