207k views
12 votes
Write a program that asks the user to enter a date in MM/DD/YYYY format that is typical for the US, the Philippines, Palau, Canada, and Micronesia. For the convenience of users from other nations, the program computes and displays this date in an alternative DD.MM.YYYY format. Sample run of your program would look like this: Please enter date in MM/DD/YYYY format: 7/4/1776 Here is the formatted date: 04.07.1776 You can assume that the user will enter correctly formatted date, but do not count on having 0s in front of one-digit dates. Hint: you will need to use string addition (concatenation) here.

1 Answer

8 votes

Answer:

In Python:

txt = input("Date in MM/DD/YYYY format: ")

x = txt.split("/")

date=x[1]+"."

if(int(x[1])<10):

if not(x[1][0]=="0"):

date="0"+x[1]+"."

else:

date=x[1]+"."

if(int(x[0])<10):

if not(x[0][0]=="0"):

date+="0"+x[0]+"."+x[2]

else:

date+=x[0]+"."+x[2]

else:

date+=x[0]+"."+x[2]

print(date)

Step-by-step explanation:

From the question, we understand that the input is in MM/DD/YYYY format and the output is in DD/MM/YYYY format/

The program explanation is as follows:

This prompts the user for date in MM/DD/YYYY format

txt = input("Date in MM/DD/YYYY format: ")

This splits the texts into units (MM, DD and YYYY)

x = txt.split("/")

This calculates the DD of the output

date=x[1]+"."

This checks if the DD is less than 10 (i..e 1 or 01 to 9 or 09)

if(int(x[1])<10):

If true, this checks if the first digit of DD is not 0.

if not(x[1][0]=="0"):

If true, the prefix 0 is added to DD

date="0"+x[1]+"."

else:

If otherwise, no 0 is added to DD

date=x[1]+"."

This checks if the MM is less than 10 (i..e 1 or 01 to 9 or 09)

if(int(x[0])<10):

If true, this checks if the first digit of MM is not 0.

if not(x[0][0]=="0"):

If true, the prefix 0 is added to MM and the full date is generated

date+="0"+x[0]+"."+x[2]

else:

If otherwise, no 0 is added to MM and the full date is generated

date+=x[0]+"."+x[2]

else:

If MM is greater than 10, no operation is carried out before the date is generated

date+=x[0]+"."+x[2]

This prints the new date

print(date)

User Grummle
by
2.9k points