-
Notifications
You must be signed in to change notification settings - Fork 0
/
bite128.py
27 lines (20 loc) · 1.08 KB
/
bite128.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from datetime import datetime, timedelta
THIS_YEAR = 2018
def years_ago(date):
"""Receives a date string of 'DD MMM, YYYY', for example: 8 Aug, 2015
Convert this date str to a datetime object (use strptime).
Then extract the year from the obtained datetime object and subtract
it from the THIS_YEAR constant above, returning the int difference.
So in this example you would get: 2018 - 2015 = 3"""
date = THIS_YEAR - (datetime.strptime(date, "%d %b, %Y")).year
return date
def convert_eu_to_us_date(date):
"""Receives a date string in European format of dd/mm/yyyy, e.g. 11/03/2002
Convert it to an American date: mm/dd/yyyy (in this case 03/11/2002).
To enforce the use of datetime's strptime / strftime (over slicing)
the tests check if a ValueError is raised for invalid day/month/year
ranges (no need to code this, datetime does this out of the box)"""
date = datetime.strftime(datetime.strptime(date, "%d/%m/%Y"), "%m/%d/%Y")
return date
print(years_ago('12 Aug, 2010'))
print(convert_eu_to_us_date('11/03/2002'))