File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
days/01-03-datetimes/code Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments