Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate age of person in months if under 1 Y.O. 1

Status
Not open for further replies.

kyletreyfield

Technical User
Jun 12, 2008
27
US
I've gone through dozens of threads on here on this topic, and I realize its an old, very basic one, but I cannot find an easy way to calculate a person's age in months based on their DOB if they are under 1 year old. For instance, if today is 1/8/2023, and a person was born on 9/27/2022, they would be 3 months old because they haven't reached January 27 yet.

How would I do this in VFP?
 
Code:
LParameters tdDOB

Local lnDays
lnDays = Date()-tdDOB
lnMonths = Floor(lnDays*12/365.25) && very good estimate

* lnMonths is the solution if
* GoMonth(tdDOB, lnMonths)<=Date()<GoMonth(tdDOB, lnMonths+1)

* The following will check that and fix it in case it's off:
* a) fix when lnMonths is too high:
Do While GoMonth(tdDOB, lnMonths)>Date()
   lnMonths = lnMonths-1
EndDo
* b) fix when lnMonths is too low:
Do While GoMonth(tdDOB, lnMonths+1)<=Date()
   lnMonths = lnMonths+1
EndDo
* lnMonths could be off by 1 at most in timespans of 100 years
* but doing while loops ensures you move the estimate
* to the exact number of months, even if it wold be off by 2 or more.
* This even works with tdDOB>Date() and will result in a negative age
* You cold also disallow negative results, if you like with the simple
* check whether tdDOB>Date() or whether lnDays<0
Return lnMonths

You have to pass in a date into this PRG and you get the ageinmonths, including negative months in the case DOB is in the future. You can adjust it, if you don't like that, see code comments.

Chriss
 
Hi,

Did you have a look at thread184-1817993

hth

MarK
 
Chris has given you a good answer. I can't add to that.

However, for future reference, you might like to bookmark this page:


It contains an excellent batch of date-manipulation functions of various kinds, written by Rick Borup. It does not directly address your question, but by studying the code you will get some useful insights into ways of manipulating dates within VFP.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Figured I would bite the bullet by adding another submit button so I can reply.

As in the last thread I replied to, the solution I use is another one-liner.

faq184-8777

According to that function I am 912 months old.

Like everything else there are ALWAYS many ways to solve problems in FoxPro. However, due to the fact that I am limited to FPM 2.6b, MOST other ways are unworkable for me. And by my definition if functions don't work in ALL versions of FoxBase, FoxPro, and VFP they are not universal functions. To the best of my knowledge all code I write works everywhere in the FP world. May not be the prettiest code, but it works.

I only maintain/update one FP program, the one that builds and maintains my website of over 16 thousand pages: as well as the underlying databases. That site puts food on my table and pays my bills so anything I do in FP is geared to make that easier to do. At my age, I have no inclination of wasting time upgrading to a modern system.

For the record, I am in Washington State just like my profile shows.

Since I come from the Apple BASIC world with my first computer having only 16K of memory, I learned early on that if I did not think outside of the box the computer was useless. As such, I was forced to be a minimalist, doing the 'impossible' in order force the computer to do my job.

I remember one time finding a 250-line Apple BASIC program in Nibble. I typed the whole thing in, got it working, then began optimizing it since it took up too much space and was too slow. When I got done, the program was THREE lines long, faster by a factor of 10, and had added functionality not in the original Nibble program.

Because of my Apple BASIC experiences, I look at everything from a minimalist standpoint. In my world, simpler is almost always better and is usually much easier to maintain since I try to do it right once, then keep using the same code over and over. No unmaintainable spaghetti code here.

Also, due to the limits of FPM 2.6b, I cannot use long descriptive variable names (10 char limit) nor long file names (8 char limit) so some of my code is quite cryptic.

Adding a submit button is a PITA to say the least. If I make a mistake I have to start all over from scratch. Since my last post I have been thinking about writing a short FP program to do the grunt work for me. So far, I have not figured out a way to reliably do that.

I have been working on a reply to the last post I made in November. It will include screenshots showng what I see and what the page looks like with the added submit button. For the record, some TT pages have submit buttons that work fine, just not the forums nor the FAQs.

I have been lurking for years since the submit button disappeared in about 2016. Every once in a while I have given stars to deserving posts. If you got an star from an unknown person, it MIGHT have been me.

I could not sleep, so I figured I would try posting again.

mmerlinn


Poor people do not hire employees. If you soak the rich, whom are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top