random date python

Solutions on MaxInterview for random date python by the best coders in the world

showing results for - "random date python"
Dounia
29 Feb 2017
1from random import randrange
2from datetime import timedelta
3
4def random_date(start, end):
5    delta = end - start
6    int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
7    random_second = randrange(int_delta)
8    return start + timedelta(seconds=random_second)
9
10from datetime import datetime
11d1 = datetime.strptime('1/1/2008 1:30 PM', '%m/%d/%Y %I:%M %p')
12d2 = datetime.strptime('1/1/2009 4:50 AM', '%m/%d/%Y %I:%M %p')
13print(random_date(d1, d2))