How To Create Age Calculator Using Python Programming Language

Namaste Friends,
                                         Today,s Python Script Is Very Easy But Useful. Because Many Times, We Need To Know On Which Day You Born. And What Is Your Correct Age.

So, Let's Talk About Today's Topic

Today, For This Purpose We Will Use Python datetime module.

Here, I am Sharing My Demo Codes But If You Want More Better Example Then, You Can Modify these codes yourself or Download This Script From My GitHub repository (link given at the end of these codes ).

1. dob.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ---------------- READ ME ---------------------------------------------
# This Script is Created Only For Practise And Educational Purpose Only
# This Script Is Created For http://bitforestinfo.blogspot.in
# This Script is Written By
__author__='''

######################################################
                By S.S.B Group                          
######################################################

    Suraj Singh
    Admin
    S.S.B Group
    surajsinghbisht054@gmail.com
    http://bitforestinfo.blogspot.in/

    Note: We Feel Proud To Be Indian
######################################################
'''
print __author__

# Import Module
import re
import datetime

# pattern For Checking Valid Time Format
pattern = "(\d{1,2})/(\d{1,2})/(\d\d\d\d).(\d{1,2}):(\d{1,2}):(\d{1,2})" 

#  ++++ Checking Mechanism ++++++++++
# Hour
def check_h(h):
 if h==24:
  h=0
 else:
  pass
 return h # hour

# Minutes
def check_m(m):
 if m==60:
  m=0
 else:
  pass
 return m  # Minute

# Seconds
def check_s(s):
 if s==60:
  s=0
 else:
  pass
 return s 

# Main Function
def main(data):
 # Matching Pattern
 if re.match(pattern, data):
  data=[i.rjust(2, '0') for i in re.match(pattern, data).groups()]
 else:
  data=None
 return data

def age(data):
 input_time=data
 try:
  d = datetime.date(int(input_time[2]),
   int(input_time[1]), 
   int(input_time[0]))
   # Time
  t = datetime.time(check_h(int(input_time[3])),
   check_m(int(input_time[4])),
   check_s(int(input_time[5])))
  # Combine Date And Time
  b_dt = datetime.datetime.combine(d,t) 
  # Today Time
  t_dt = datetime.datetime.today()
  return [abs(t_dt-b_dt).days,b_dt,t_dt]

 except EOFError as e:
  print "[*] Something Wrong.."
  print "[*] Friend... ", e
  return 
  

if __name__ == '__main__':
 formated_time = main(raw_input("[+] Enter Date In dd/mm/year hh:mm:ss "))  # Formated Date
 print datetime.timedelta(days=age(formated_time)[0]).__str__()

if you want with Gui(Graphical User Interface) Then Check This.


and here, its python code.

2. main.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
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# ---------------- READ ME ---------------------------------------------
# This Script is Created Only For Practise And Educational Purpose Only
# This is an Example Of Tkinter Canvas Graphics
# This Script Is Created For http://bitforestinfo.blogspot.in
# This Script is Written By
__author__='''

######################################################
                By S.S.B Group                          
######################################################

    Suraj Singh
    Admin
    S.S.B Group
    surajsinghbisht054@gmail.com
    http://bitforestinfo.blogspot.in/

    Note: We Feel Proud To Be Indian
######################################################
'''
print __author__
try:
 import Tkinter
except:
 import tkinter as Tkinter
import dob

# Format
printdate = """    
Date   : \t{0:%d}
Month  : \t{0:%B}
Year   : \t{0:%Y} 
Day    : \t{0:%A} 
Hour   : \t{0:%I}
Minute : \t{0:%M}
Second : \t{0:%S}
Period : \t{0:%p}

Standard Date   
   {0:%d}/{0:%B}/{0:%Y} {0:%A} {0:%I}:{0:%M}:{0:%S} {0:%p}
"""
printage="""Age
  In Days : {} Days
  In Year : {} Years {} days
"""


def main():
 root=Tkinter.Tk(className=" Age Calculator")
 storeobj=MFrame(root, text="Input Fields")
 storeobj.pack(padx=20,pady=20,ipadx=20,ipady=20,expand="yes",fill='both')

 storeobj1=NFrame(root, text="Output Fields")
 storeobj1.pack(padx=20,pady=20,ipadx=20,ipady=20,expand="yes",fill='both')
 main_variable = Tkinter.StringVar()
 main_variable.set(printdate)
 storeobj.main_variable = main_variable 
 storeobj1.main_variable = main_variable
 storeobj1.create_label()
 root.mainloop()
 return

class MFrame(Tkinter.LabelFrame):
 def __init__(self, *args, **kwargs):
  Tkinter.LabelFrame.__init__(self,*args, **kwargs)
  self.main_variable=None
  self.all_function_trigger()

 def all_function_trigger(self):
  self.create_variables()
  self.create_labels_()
  self.create_date_spin_box()
  self.create_time_spin_box()
  self.create_excecute_button()
  return

 def run_command(self):
  lt=[]
  for i in self.variables:
   lt.append(i.get())
  data= [i.get().rjust(2,"0") for i in self.variables]
  age,born,today = dob.age(data)
  self.main_variable.set(printdate.format(born)+printage.format(age,str(divmod(int(age),365)[0]),str(divmod(int(age),365)[1])))
  print self.main_variable.get()
  return

 def create_excecute_button(self):
  Tkinter.Button(self,text="Calculate",width=15, command=self.run_command, relief="raised").grid(row=10, column=0, columnspan=6)
  return

 def create_variables(self):
  self.variables=[]
  for i in [1,1,1970,1,59,59]:
   storeobj=Tkinter.StringVar()
   storeobj.set(i)
   self.variables.append(storeobj)
  return

 def create_date_spin_box(self):
  Tkinter.Label(self, text="dd/mm/yyyy").grid(row=1,column=0)
  Tkinter.Spinbox(self, from_=1,to_=31, width=3, textvariable=self.variables[0]).grid(row=1, column=1)
  Tkinter.Label(self, text="/").grid(row=1, column=2)
  Tkinter.Spinbox(self, from_=1,to_=12, width=3, textvariable=self.variables[1]).grid(row=1, column=3)
  Tkinter.Label(self, text="/").grid(row=1, column=4)
  Tkinter.Entry(self, width=6, textvariable=self.variables[2]).grid(row=1, column=5)
  return

 def create_time_spin_box(self):
  Tkinter.Label(self, text="hh:mm:ss").grid(row=5,column=0)
  Tkinter.Spinbox(self, from_=1,to_=24, width=3, textvariable=self.variables[3]).grid(row=5, column=1)
  Tkinter.Label(self, text=":").grid(row=5, column=2)
  Tkinter.Spinbox(self, from_=1,to_=60, width=3, textvariable=self.variables[4]).grid(row=5, column=3)
  Tkinter.Label(self, text=":").grid(row=5, column=4)
  Tkinter.Spinbox(self, from_=1,to_=60, width=3, textvariable=self.variables[5]).grid(row=5, column=5)
  return

 def create_labels_(self):
  Tkinter.Label(self, text="Date").grid(row=0, column=0, columnspan=4)
  Tkinter.Label(self, text="Time").grid(row=3, column=0, columnspan=4)
  return

class NFrame(Tkinter.LabelFrame):
 def __init__(self, *args, **kwargs):
  Tkinter.LabelFrame.__init__(self, *args, **kwargs)
  self.main_variable=None
  

 def create_label(self):
  Tkinter.Label(self, textvariable=self.main_variable, justify="left").pack()
  return




if __name__ == '__main__':
 main()

For Downloading Raw Scripts Click here

For Video Check This

For More Update, Visit Our Regularly. 

And Subscribe Our Blog, 

Follow Us and share it.
For Any Type of Suggestion Or Help
Contact me:
S.S.B
surajsinghbisht054@gmail.com 

Share this

Related Posts

Previous
Next Post »