Skip to content

Commit 1b2bf00

Browse files
committed
#
1 parent 4b03e23 commit 1b2bf00

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

datetime_example/datetime_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
from datetime import date
99

10+
1011
if __name__ == '__main__':
1112
# dates are easily constructed and formatted
1213
now = date.today()
1314
print(now)
1415
print(now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B."))
1516

16-
1717
# dates support calendar arithmetic
1818
birthday = date(1964, 7, 31)
1919
age = now - birthday
20-
print(age.days)
20+
print(age.days)

datetime_example/parse_text.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
__author__ = 'ipetrash'
5+
6+
7+
from datetime import datetime
8+
9+
10+
def get_age(text):
11+
date_text = text.split()[0]
12+
dt = datetime.strptime(date_text, '%d/%m/%Y')
13+
years_delta = datetime.today() - dt
14+
15+
return int(years_delta.days / 365)
16+
17+
18+
if __name__ == '__main__':
19+
text = '31/07/1972 (45 years old)'
20+
age = get_age(text)
21+
print(age) # 45
22+
23+
text = '18/08/1992'
24+
age = get_age(text)
25+
print(age) # 25

0 commit comments

Comments
 (0)