Topic: A ONE LINE AGE CALCULATION.
Reply
Mefikit's photo

Mefikit

Fri 10/09/20 12:14 PM

This was written for Access basic, about 25 years ago. It shouldn't be difficult for someone with rudementary programming skills to convert the code into something usable. A guy who plagerised my code back then (without any reference to my work) converted it to Visual Basic.

I was browsing a Visual Basic website, looking for free software, when I found a site covering the calculation of a person's age from their date of birth. There were over 50 submissions and I admit I didn't have time to read them all, but what I did notice with the ones that I read, was that some of the calculations were only approximate. Some of them even counted the number of days from the birth date to the present day and then divided the total by 365.25 (one year plus a quarter day, to allow for leap years).

Here is a one line piece of code that is accurate to the day. Copy and paste these three lines of code into your visual basic debug or immediate windows and alter the date of birth to see the changes.

DOB=#26/10/1903# 'variable DOB holds the date of birth, which needs the hash (#) markers.

Age = (DatePart("yyyy", Date) - DatePart("yyyy", DOB)) + (Date < (DateSerial(DatePart("yyyy", Date), DatePart("m", DOB), DatePart("d", DOB)))) 'these lines are actually one line.

PRINT Age

A quick look at the workings of the code above.

DOB = a user defined variable. DOB means nothing, I could have called it "housebrick".
AGE = a user defined variable. Again this could have been any name, e.g. "YearsOld".

Now to the nitty-gritty.

DatePart = a visual basic defined function. It can be used to extract days, quarters, weeks etc. In the example above I have used it to extract the Year Number from "Date".

Date = a visual basic defined function. It is used to pull the date part from your computer's system clock. So, DatePart("yyyy", Date) will return 2003 for this year, or whatever year you are trying this piece of code.

The same functions are used to calculate the year number of the date of birth. I have used 1903 as the year to make checking the calculation easier. The (DOB) year number is subtracted from the (Date) year number, i.e. 2003-1903 the answer = 100. Now, that is the easy part. The next part is to make the code recognise the actual day that the birthday falls.

This part uses a function that is built into all programming codes, i.e. "True" and "False"
TRUE = -1 and FALSE = 0. That is minus one for TRUE and zero for FALSE. We can use this to adjust the AGE right down to the day.

After the code has calculated the "Year" difference between the DOB and TODAY, there has to be a way for the code to recognise that, although the year difference is correct, the actual date of the subject's birthday hasn't arrived yet. Example, using a 100 year old person born on the 1st of June, 1903 that person isn't actually 100 until 1st of June, 2003. Dates before the 1st of June make the person only 99. So, the tricky piece of code to adjust this uses the value -1 TRUE, if the Month and Day of this year is less than the Month and Day of the birthday year. What the code does is "makes a statment" that can be checked against either "TRUE" or "FALSE".
For instance: BLACK = WHITE would return FALSE which equals 0 (zero)
BLACK = BLACK would return TRUE which equals -1 (minus one) or even
7 < 10 (seven is less than ten) which is TRUE = -1
The statement the code uses can be broken down into manageable chunks if I describe it in smaller detail. After the year difference has been calculated, the TRUE/FALSE statement is added.
+ (Date < (DateSerial(DatePart("yyyy", Date), DatePart("m", DOB), DatePart("d", DOB))))

PLUS (+) (we are going to add the TRUE or FALSE value to the year difference calculation).
Date (returns the date taken from the computer's system clock)
< is less than
DateSerial visual basic function, to convert numbers into a date.
In the order: year,month,day e.g. 1st June 1902 = DateSerial(1902,6,1)
Notice that the code creates a date that is made up from the year number of this year and the month and day from the date of birth. This is so that the calculation (or statement) is only concerned with the month and day values.
DatePart Is explained above.

Basically the statement says that if the Month and Day from this year is less than the Month and Day from the date of birth (in other words, the birthday has yet to come) then the answer is TRUE = -1, so, the code adds -1 to the calculation. Converting 100 to 99. If on the other hand if the Month and Day values are the same (today is the birthday) then the statement is FALSE = zero, so zero is added to 100 the result is 100. The same applies to dates after the birthday which will calculate as FALSE.

A very complicated description, I'm sorry, but if you use the code and don't worry about how it works, who cares. I am sure that this is not the only possible calculation for age, in fact it is such a common function of most personal details databases, that it must have been "cracked" a long time ago. I am really surprised that visual basic code still does not have a function called "AGE" that will do all the work for us code writers.
Mefikit's photo

Mefikit

Sat 10/10/20 03:20 AM

This is just a "By-the-way" in an attempt to avoid any confusion, caused by anyone actually wanting to test the TRUE/FALSE statement, mentioned above. i.e. BLACK = WHITE = FALSE.
You might be surprised to find, that in computer parlance, the expression - BLACK = WHITE is actually TRUE. Also, so is the expression - CHALK = CHEESE.
No magic involved. It is simply that the program interpreter sees the name BLACK as a user defined variable, with a probable value of NULL. Also WHITE is also seen as having a value of NULL. Therefore, they are, in fact both equal and the statement BLACK = WHITE is TRUE.