Can calculate your age (in years and months), and even takes the LeapYear into account. Only uses a few simple functions... Fully functional
API Declarations
' *********************************************
' * Coded by: '][' o r p i d ]P r e y *
' * [email protected] *
' *********************************************
'==================Instructions==================
'Include Anywhere, just read 'em ffs
'A lot of this code is just making it look presenentable. It is not all necessary, but the
'functions in the module below are!
'Use this code however you want just don't be a dipsh*t and pretend that you wrote it
'This is an easy way to calculate someones age. Place the following items anywhere on the form
'Label1
'Label2
'Text1
'Command1
'I know it is easier to select a date from the DTPicker but this saves people dicking around
'with components when they just want the functions, cuz I hate it when other people do that.
'This should work from just a standard form with 2 labels, a text box and a command button
'all with their default names
'Make the project start in Sub Main, and change;
'Form1.Maxbutton = False
'Form1.Borderstyle = 2 - Sizable
'==================Declarations==================
'In Module
Option Explicit
Public BDay As Date
Public Months As Integer
Public Const DAYSinYEAR As Integer = 365.25
'==================Functions==================
'In Module
Public Function Age(Birthday As String) As Single
' ^
'*** 'Birthday is declared as a String beacause I am using a textbox...
'You can change it to Birthday as Date if you are going to pre-formatted
'dates rather than string text dates such as that from a DTPicker...
Dim Days As Long
Days = DateDiff("d", Birthday, Date)
If IsLeapYear(Year(BDay)) = True Then
'If the Birthday is before LeapYearInstance for the same year, then... else...
If DateDiff("d", LeapYearInstance(BDay), BDay) < 0 Then
' Prior to Feb 29
Else
' Feb 29 or Later
Days = Days + 1
End If
End If
Months = AgeMonths(Days / DAYSinYEAR)
Age = Int(Days / DAYSinYEAR)
End Function
Public Function AgeMonths(AgeYears As Single) As Integer
AgeMonths = (AgeYears - Int(AgeYears)) * 12
End Function
Public Function IsLeapYear(Yr As Integer) As Boolean
Dim i As Byte
IsLeapYear = True
On Error GoTo No_29th_Feb
'You need to try to obtain it to set off the Error
i = Weekday("29/02/" & Yr)
Exit Function
No_29th_Feb:
IsLeapYear = False
End Function
Public Function LeapYearInstance(Birthday As Date) As Date
LeapYearInstance = Format("Feb 29 " & Year(Birthday), "MMM dd yyyy")
End Function
Private Sub Main()
Form1.BorderStyle = 1
Form1.Show
End Sub