How to take a date from the user?

Question:

How would I go about designing a Python program which takes in a date from the user, a date that looks like this 3/13/17, and turns it into a date which looks like this 2017.3.13?

Asked By: Suikurix

||

Answers:

Get the date as text and then convert it to date with the format you would like. or get the date as a number (month, date and year ) separately and make it as a date.
Example:

my_string = str(input('Enter date(yyyy-mm-dd): '))
my_date = datetime.strptime(my_string, "%Y-%m-%d")
Answered By: VKK

You can split the string by using the str.split method like this:

s = "3/13/17"
month, day, year = s.split("/")
print(f"20{year}.{month}.{day}")

Python will automatically assign the splitted values to the variables month, day, and year

Answered By: The Peeps191
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.