Skip to content

Commit e8e3b32

Browse files
committed
Pybite challenge 128 Strptime and strftime
1 parent c99fe5d commit e8e3b32

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from datetime import datetime
2+
3+
THIS_YEAR = 2018
4+
5+
6+
def years_ago(date):
7+
"""Receives a date string of 'DD MMM, YYYY', for example: 8 Aug, 2015
8+
Convert this date str to a datetime object (use strptime).
9+
Then extract the year from the obtained datetime object and subtract
10+
it from the THIS_YEAR constant above, returning the int difference.
11+
So in this example you would get: 2018 - 2015 = 3"""
12+
date_time = datetime.strptime(date.replace(",", ""), "%d %b %Y")
13+
diff_year = THIS_YEAR - date_time.year
14+
return diff_year
15+
#pass
16+
17+
18+
def convert_eu_to_us_date(date):
19+
"""Receives a date string in European format of dd/mm/yyyy, e.g. 11/03/2002
20+
Convert it to an American date: mm/dd/yyyy (in this case 03/11/2002).
21+
To enforce the use of datetime's strptime / strftime (over slicing)
22+
the tests check if a ValueError is raised for invalid day/month/year
23+
ranges (no need to code this, datetime does this out of the box)"""
24+
25+
eu_date = datetime.strptime(date.replace("/" , " "), "%d %m %Y")
26+
27+
28+
us_date = eu_date.strftime("%m") + "/" + eu_date.strftime("%d") + "/" + eu_date.strftime("%Y")
29+
30+
return us_date
31+
32+
print(convert_eu_to_us_date('11/03/2002'))
33+

0 commit comments

Comments
 (0)