In: Computer Science
#Python 3.7 "Has no attribute" error -
def get():
import datetime
d = date_time_obj.date()
return(d)
print(a["Date"])
print("3/14/2012".get())
How to write the "get" function (without any imput), to convery the format ""3/14/2012" to the format "2012-03-14", by simply using "3/14/2012".get() ?
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Solution 1 :Without Import datetime
Here a new python program with name "main.py" is created, which contains following code.
main.py :
#function
def get(date):
#split date based on /
date=date.split("/")
#concate elements from list
date=date[2]+"-"+str(0)+date[0]+"-"+date[1]
return date #retun date
#function call to print date
print(get("3/14/2012"))
======================================================
Output : Compile and Run main.py to get the screen as shown below
Screen 1 :main.py
********************
Solution 2 :With import datetime
main.py :
import datetime #import
#pass date and date format
d = datetime.datetime.strptime("3/14/2012", '%m/%d/%Y')
#print date format
print(datetime.date.strftime(d, "%Y-%m-%d"))
===========================
Screen 1 :
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.