Home > other >  How to take a date from the user?
How to take a date from the user?

Time:07-22

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?

CodePudding user response:

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

CodePudding user response:

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")
  • Related