Skip to main content

How to Create Dates from User Input in Python

This guide explains how to create date and datetime objects in Python using user-provided input. We'll cover taking separate year, month, and day inputs, and also how to handle a single formatted date string. We'll also include input validation.

Creating date Objects from Separate Inputs

To create a date object from individual year, month, and day inputs, prompt the user for each component separately, convert them to integers, and then pass them to the date() constructor.

from datetime import date

year = int(input('Enter a year: '))
month = int(input('Enter a month: '))
day = int(input('Enter a day: '))

d = date(year, month, day)
print(d)

Example Usage:

Enter a year: 2024
Enter a month: 5
Enter a day: 15
2024-05-15
  • The input() function is used to get the values from the user as strings.
  • The int() constructor converts the strings to integers, since years, months and days are numeric values.
  • The values are passed to the date constructor.

Creating datetime Objects from Separate Inputs

To create a datetime object, collect inputs for hours, minutes, and seconds in addition to the date components:

from datetime import datetime

year = int(input('Enter a year: '))
month = int(input('Enter a month: '))
day = int(input('Enter a day: '))
hours = int(input('Enter the hour: '))
minutes = int(input('Enter the minutes: '))
seconds = int(input('Enter the seconds: '))

dt = datetime(year, month, day, hours, minutes, seconds)
print(dt)

Example:

Enter a year: 2023
Enter a month: 12
Enter a day: 31
Enter the hour: 23
Enter the minutes: 59
Enter the seconds: 59
2023-12-31 23:59:59

Creating date Objects from a Formatted String

If the user provides a date in a specific format (e.g., "YYYY-MM-DD"), you can parse it directly.

from datetime import date

date_components = input('Enter a date formatted as YYYY-MM-DD: ').split('-')
year, month, day = [int(item) for item in date_components]

d = date(year, month, day)
print(d)

Example

Enter a date formatted as YYYY-MM-DD: 2024-01-15
2024-01-15
  • The split('-') method splits the input string into a list of strings.
  • The list comprehension iterates through the list returned by split() to convert each element to an integer using the int() constructor.
  • The year, month, and day are unpacked into separate variables and passed as arguments to the date() constructor.

Input Validation (Important)

The examples above assume the user enters valid input. In a real-world application, always validate the input to prevent errors:

from datetime import date

while True:
try:
year = int(input('Enter a year: '))
month = int(input('Enter a month (1-12): '))
day = int(input('Enter a day (1-31): '))
d = date(year, month, day) # Attempt to create the date
print(d)
break # Exit the loop if successful
except ValueError:
print("Invalid date. Please enter valid year, month, and day values.")
  • This code uses a while True loop to repeatedly ask for input until a valid date is entered.
  • The try...except ValueError block handles potential errors if the user enters non-numeric values or an invalid date (e.g., February 30th).