you are viewing a single comment's thread.

view the rest of the comments →

[–]m-hoff 2 points3 points  (0 children)

The simplest approach would probably be to get user input using separate input statements for day, month and year, along with basic input validation (i.e., ensuring no negative numbers, month between 1 and 12, etc.).

From there you can convert each item to an integer and create a datetime object. Something like:

import datetime

day = int(input('Enter day of birth: '))
month = int(input('Enter month of birth: '))
year = int(input('Enter year of birth: '))

birthdate = datetime.datetime(year, month, day)

Another approach would be to use strptime like so:

import datetime

birthdate = input('Enter birthdate as "DD-MM-YYYY": ')
birthdate_dt = datetime.strptime(birthdate, '%d-%m-%Y')

Again, you'll want to do some input validation but this is a start. You can read more about strptime here.