Python Math: Select a random date in the current year
74. Random Date in Current Year
Write a Python program to select a random date in the current year.
Sample Solution:
Python Code:
from datetime import date
import random
start_dt = date.today().replace(day=1, month=1).toordinal()
end_dt = date.today().toordinal()
random_day = date.fromordinal(random.randint(start_dt, end_dt))
print(random_day)
Sample Output:
2017-02-19
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to generate a random date in the current year using datetime and random modules, and print the date in YYYY-MM-DD format.
- Write a Python function that returns a random date within the current year and verifies that the date falls within January 1 and December 31.
- Write a Python script to generate several random dates in the current year and output them in sorted order.
- Write a Python program to prompt the user for a year and then output a random date from that year.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to generate (given an integer n) a square matrix filled with elements from 1 to n2 in spiral order.
Next: Write a Python program to calculate clusters using Hierarchical Clustering method.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.