Q:

Generate a random date between given start and end dates using python programming

belongs to collection: Random Data Generation Exercises

0

Generate a random date between given start and end dates

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import random
import time

def getRandomDate(startDate, endDate ):
    print("Printing random date between", startDate, " and ", endDate)
    randomGenerator = random.random()
    dateFormat = '%m/%d/%Y'

    startTime = time.mktime(time.strptime(startDate, dateFormat))
    endTime = time.mktime(time.strptime(endDate, dateFormat))

    randomTime = startTime + randomGenerator * (endTime - startTime)
    randomDate = time.strftime(dateFormat, time.localtime(randomTime))
    return randomDate

print ("Random Date = ", getRandomDate("1/1/2016", "12/12/2018"))

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< Roll dice in such a way that every time you get th...