Last active
February 18, 2023 15:09
-
-
Save rg3915/db907d7455a4949dbe69 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() |
Thanks for sharing! ! Works nice!
thank you
Top sweet
Use pip install randomtimestamp
how would you get it in the form DD/MM/YYYY currently 1971-08-18 18:24:09.033866
new_random_date = random_date.split("\n")
print(new_random_date[0])
how would you get it in the form DD/MM/YYYY currently 1971-08-18 18:24:09.033866
`
from randomtimestamp import randomtimestamp
random_date = randomtimestamp(text=False).strftime("%d/%m/%Y")
random_date = '30/05/1967' # example
`
Just thank you!
Thank you!
Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very good implemetation, thanks