Skip to content

Instantly share code, notes, and snippets.

@rg3915
Last active February 18, 2023 15:09
Show Gist options
  • Save rg3915/db907d7455a4949dbe69 to your computer and use it in GitHub Desktop.
Save rg3915/db907d7455a4949dbe69 to your computer and use it in GitHub Desktop.
Generate Random Datetime Python
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()
@norohind
Copy link

Just thank you!

@rufatpro
Copy link

rufatpro commented Jul 5, 2022

Thank you!

@hasansalimkanmaz
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment