+ 1
Help me to edit my code in the link
When I enter date in 00/00/0000 format, it should output sensible date. For example, 11/20/2000 should output 20/11/2000. https://code.sololearn.com/cCrw0rsFpEA4/?ref=app
4 ответов
+ 2
Good try pal but there are more efficient solutions...
---------------------------------------------------------------
SOLUTION 1 (Best)
from datetime import datetime
df = ['01/31/2014', '02/06/2013']
f = [datetime.strptime(d, "%d/%m/%Y").strftime("%m/%d/%Y") for d in df]
print(f)
---------------------------------------------------------------
The way you tried, just a smaller ver.
SOLUTION 2
df = ['01/31/2014', '02/06/2013']
for a in df:
s = list(a)
d = [s[3],s[4],s[2],s[0],s[1],s[5],s[6],s[7],s[8],s[9]]
f.append("".join(d))
print (f)
+ 1
Arun Bhattacharya Thanks for your honest help. But may you please explain your code!
+ 1
My Solution 1 Explanation:
from datetime import datetime
df = ['01/31/2014', '02/06/2013']
f = [datetime.strptime(d, "%d/%m/%Y").strftime("%m/%d/%Y") for d in df]
print(f)
Line 1: Importing Datetime Module
Line 2: Considering any two dates in a form of a tuple (stored in "df")
Line 3: datetime.strptime(d, "%d/%m/%Y")
-> strptime is a method in datetime
-> The first parameter "d" -> is the dates in "df"
-> strptime parses the date
-> The Second parameter parses the "%d"->The Date, "%m"-> The Month, "%y"-> The Year
.strftime("%m/%d/%Y")
-> The "." brings the earlier statement together
-> strftime is a method in datetime
-> Here the parameter is the format in which we parsed earlier, so, now "%m"-> The Month, "%d"-> The Date, "%y"-> The Year
for d in df
-> This, in simpler manner, the "d" anywhere in the statement is the "df[0]" and then "df[1]"
Line4: You understand whats "print(f)"
strptime -> strptime means string parser, this will convert a string format to datetime.
strftime -> strftime means string formatter, this will format a datetime object to string format.
0
Thanks once again