In Python can you select a random date from a year. e.g. if the year was 2010 a date returned could be 15/06/2010
Python select random date in current year
Asked Answered
It's much simpler to use ordinal dates (according to which today's date is 734158):
from datetime import date
import random
start_date = date.today().replace(day=1, month=1).toordinal()
end_date = date.today().toordinal()
random_day = date.fromordinal(random.randint(start_date, end_date))
This will fail for dates before 1AD.
+1 Very neat! I didn't know what ordinal dates was before :-) Clearly better and prettier then my timstamp-solution, which has even greater limitations on "dates before"... –
Kermie
The question was "random date from a year", not between Jan 1st and today. –
Exoergic
But the title was "select random date in current year". To get random date in an arbitrary year you just have to calculate different start and end dates, e.g.:
start_date = date(day=1, month=1, year=MY_YEAR).toordinal()
and end_date = date(day=31, month=12, year=MY_YEAR).toordinal()
. –
Joplin Just would like to add we can now also create datetime objects via
datetime.fromisoformat('2011-11-04')
which may or not be easier to type and read for you :) –
Travers Not directly, but you could add a random number of days to January 1st. I guess the following should work for the Gregorian calendar:
from datetime import date, timedelta
import random
import calendar
# Assuming you want a random day of the current year
firstJan = date.today().replace(day=1, month=1)
randomDay = firstJan + timedelta(days = random.randint(0, 365 if calendar.isleap(firstJan.year) else 364))
Nice. There's actually an
isleap()
function in the calendar
module, which would save defining it yourself. –
Edp @Daniel: Thanks, you're right. Learning something new every day on SO :) Haven't used the calendar module a lot (yet). Edited my answer. –
Exoergic
import datetime, time
import random
def year_start(year):
return time.mktime(datetime.date(year, 1, 1).timetuple())
def rand_day(year):
stamp = random.randrange(year_start(year), year_start(year + 1))
return datetime.date.fromtimestamp(stamp)
Edit: Ordinal dates as used in Michael Dunns answer are way better to use then timestamps! One might want to combine the use of ordinals with this though.
import calendar
import datetime
import random
def generate_random_date(future=True, years=1):
today = datetime.date.today()
#Set the default dates
day = today.day
year = today.year
month = today.month
if future:
year = random.randint(year, year + years)
month = random.randint(month, 12)
date_range = calendar.monthrange(year, month)[1] #dates possible this month
day = random.randint(day + 1, date_range) #1 day in the future
else:
year = random.randint(year, year - years)
month = random.randint(1, month)
day = random.randint(1, day - 1)
return datetime.date(year, month, day)
This is an old question, but, you can use my new library ^_^ chancepy
here
from chancepy import Chance
randomDate = Chance.date(year=2020)
To get a random date you can use faker
pip install faker
from faker import Faker
fake = Faker()
fake.date_between(start_date='today', end_date='+1y')
if you want from the beginning of the year then:
start_date = datetime.date(year=2023, month=1, day=1)
fake.date_between(start_date, end_date='+1y')
© 2022 - 2024 — McMap. All rights reserved.