-
-
Save diek/f2201ac06451608957d49f3be58aba14 to your computer and use it in GitHub Desktop.
Generate Random Datetime Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from datetime import datetime, timedelta | |
min_year=1900 | |
max_year=datetime.now().year | |
start = datetime(min_year, 1, 1, 00, 00, 00) | |
years = max_year - min_year+1 | |
end = start + timedelta(days=365 * years) | |
for i in range(10): | |
random_date = start + (end - start) * random.random() | |
print(random_date) | |
#done | |
# or a function | |
def gen_datetime(min_year=1900, max_year=datetime.now().year): | |
# generate a datetime in format yyyy-mm-dd hh:mm:ss.000000 | |
start = datetime(min_year, 1, 1, 00, 00, 00) | |
years = max_year - min_year + 1 | |
end = start + timedelta(days=365 * years) | |
return start + (end - start) * random.random() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment