What would be a proper formula for:

- User prompt to enter Month Day, Year -> set to DOB variable

2 Calculate (Current Date - DOB) = age -> set to Age variable.

What would be a proper formula for:

- User prompt to enter Month Day, Year -> set to DOB variable

2 Calculate (Current Date - DOB) = age -> set to Age variable.

You can use the TIME function to get the time for a specific year/month/day, and subtract that from the TIME for now, divide by the number of days in a year and the number of seconds in a day.

Note: This macro is slightly approximate, so it will likely give an off-by-one answer if you are almost on your birthday. If you need that level of precision, then you will have to go through each of the YEAR(), MONTH() and DAY() functions.

Keyboard Maestro Actions.kmactions (1.5 KB)

4 Likes

Hi Peter,

Please help me...

I need this to return the result in the format with years, months and days

For instance: 30 years, 11 moths and 2 days

Can you help me modify this?

I'm contemplating your question. There's something that I don't understand, because **a person's age cannot possibly tell you how many years, months and days old they are**. That's because it depends on when they were born. **For example**, a person born on Jan 1, who is 30 days old, is ZERO months and THIRTY days old. However a person born on Feb 1, who is 30 days old, is ONE month and TWO or THREE days old. Those are two different answers for the same age. Right?

So there is no way to convert an age in years or seconds into an age that is in terms of years, months and days. Can you see that?

**All you can do is find the difference between two dates.** That's something that CAN be determined either in terms of years, months and days or just days. Is that what you want?

If you have two dates that you want to measure the difference between, you can try to calculate the difference yourself. For a simple example, if the dates are written as YYYY-MM-DD, and the two dates are 2020-12-30 and 2010-06-15 then, in this simple case, you could subtract each corresponding number to get 10 years, 6 months, 15 days. That's not hard. The only hard part is what happens when you get a negative number. You have to "carry" a digit from the leftwards column. For example, if the month difference was -3, then you would add 12 to the month to get 9, and subtract one from the year. Carrying over from the day to the month is slightly harder, because months have differing numbers of days, and because some years are leap years. But I'll bet you could calculate the entire set of differences in three KM actions.

EDIT: WOW! I received a "Like" on this post from PeterNLewis! How exciting! That's like walking through the pearly gates! Thanks, Peter.

1 Like

I'm not actually 100% sure if KM has a date difference calculator of the type you want, but I don't think it does. Like most languages, KM measures dates in terms of **decimal seconds** or **decimal years.** It also allows you to add dates but those dates have to be in one of the decimal forms I just listed. It can convert dates to these decimal values, and it can convert decimal values to dates. It is perfectly legal to add or subtract decimal dates, and once you do, you can convert them to non-decimal dates (years, months and days.)

The **difference** between two decimal dates cannot be converted to a non-decimal date, and I gave an example in my previous post why this is not possible. However if you have two non-decimal dates, you can use some logic to calculate the difference between them as I indicated in my previous post. I'm going to post an example here, HOWEVER after I wrote this example I realized I forgot about leap years, so this code does NOT work accurately across any date pair that includes one or more leap years. I may fix that, but at this point **I'm just showing the code as an illustration** of the kind of arithmetic that needs to be done to solve this problem. The green code is where the arithmetic occurs, and the yellow code is where my leap year bug needs to be fixed.

(P.S. I did say I thought that the whole calculation could be done in three actions. This code is NOT in three actions because I want clarity. I still think it's possible to write this in three actions, but that is not what my goal was today.)

Date Difference Calculator Macro (v10.0.2)

Date Difference Calculator.kmmacros (8.5 KB)

An added complication is, of course, that after a certain stage of life, age ceases to be a simple function of birth date and *anno domini*.

( Complicating factors such as discretion, creativity and imagination begin to come into play, while the precise year of birth grows hazier and less certain with distance. I speak purely from experience )

Perhaps you could introduce a random variable, or implement fuzzy numbers ?

1 Like

In addition to **leap years**, there is something most people don't know about called **leap seconds**. Some years have a second added or subtracted from them. Thus, for an accurate translation from decimal seconds to Years, Months and Days, one would have to account for this. They work somewhat similarly to a leap year, but unlike leap years they aren't **predictable**. Taking this into account may result in a solution that takes more than three actions to perform.

Curiously, this also means future dates (far in the future) cannot be predicted with accuracy. Since there are 31 million seconds in a year, PLUS OR MINUS ONE each year, then in (about) 31 million years it could be either Feb 14 or Feb 15. We can't possible know, due to the unpredictability of leap seconds.