File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77
88from datetime import date
99
10+
1011if __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 )
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments