1. Jure Gornjak
  2. PowerBuilder
  3. Sunday, 1 December 2019 20:00 PM UTC
PB programmers,

Please help calculate age between dates:

31.August.1993 and 01.March.2019 !

Thank you and best regards
Subin Subash Accepted Answer Pending Moderation
  1. Monday, 13 January 2020 17:46 PM UTC
  2. PowerBuilder
  3. # 1

Hi Jure,

 

  Can you please try the below logic,

 

If ( birth month > current month )
    age = ( curr year - ( birth year + 1 ) )
- if birth month = current month and birth day >= currDay
    age =( curr year - ( birth year + 1 ) )
- otherwise
    age = ( curr year - birth year )
Comment
There are no comments made yet.
Ricardo Jasso Accepted Answer Pending Moderation
  1. Monday, 2 December 2019 15:04 PM UTC
  2. PowerBuilder
  3. # 2

Michael,

That's a question that cannot be answered precisely. One month prior to 28-Feb-2019 could be 31-Jan-2019 for a house rent that has to be paid at the end of each month. Or it could be 28-Jan-2019 for a car lease payment due date because that's how it was specified in the contract, the obligation to pay the 28th of each month. Trying to add or substract months is not straight forward because each month has a different number of days, so you need to specify how do you want to handle months. Maybe you want to use the average number of days a month has which is 30.44. Some financial applications use 30.5, for example, when quoting a car lease. But when calculating the actual interest they calculate the number of days between the last and next due dates. So, the first thing will be to specify how do you want to express a person's age and how it should be expressed if you want to use months. What I'm trying to say is you are going to need several If...Then statements in your code to reflect the desired rules.

Regards,

Ricardo

Comment
  1. Michael Kramer
  2. Monday, 2 December 2019 16:05 PM UTC
I totally agree. As written in prior answers in other threads: You *MUST* know the business rules before writing the code.

When you have resource constraint environment, you measure in "precise" units like days or hours instead of rubberband-ish units like months and years. Travel to Mars you don't want to be 3 days short of food. Likewise crossing Sahara or Antarctica.

Air travel calculates in UTC to for same reason. You don't want a Boeing 747 or Airbus A380 acting as glider for 60 minutes just because daylight savings ended during the flight.
  1. Helpful
There are no comments made yet.
Michael Kramer Accepted Answer Pending Moderation
  1. Monday, 2 December 2019 12:44 PM UTC
  2. PowerBuilder
  3. # 3

Hi All, starting new reply to retrace back to reason for initial reply.

From a computer science perspective I prefer using functions where the reverse function on function's return value returns the original value. 

Let us therefore discuss function "add M months; then add D days." Inverse function therefore is "subtract D days; then subtract M months."

When that is my premise I cannot add full months when initial input has day-of-month = 29 or 30 or 31. When an out-of-month day is reduced to end-of-month, I cannot reverse from output to input.

Example (in case of this question): What is "one month prior to 28-Feb-2019?

And THAT is where the root of calendar calculation issues originates?

  1. How do you handle end-of-month?
  2. Do you care whether x = f1( f(x) ) where f1 = reverse of "f"?

There are very good reasons for international finance where I currently work to expres everything as number of days instead of years and months. 

HTH /Michael 

Comment
There are no comments made yet.
Jure Gornjak Accepted Answer Pending Moderation
  1. Sunday, 1 December 2019 20:48 PM UTC
  2. PowerBuilder
  3. # 4

Thanks Michael ,

What do you think of my experiment ?
 
Initial date - none - 31-Aug-1993
Add 25 years 25Y 31-Aug-2018
Add 5 months 25Y, 5M 31-Jan-2019
Add 1 months 25Y, 6M 28-Feb_2019
Add 1 more day 25Y, 6M, 1D 01-Mar-2019

So my answer is 25 years, 6 months, and 1 day.

Best regard

Jure

 

Comment
  1. Ricardo Jasso
  2. Monday, 2 December 2019 08:00 AM UTC
Michael,



The problem with age calculations is that we tend to treat days and months as cardinal numbers when they are ordinal numbers which tell the position of something in a list, not how many there are. For example, March is the 3rd month of the year, not 3 months.



We should first calculate the number of cardinal days between two dates using the function DaysAfter(date1, date2) and then count first the number of ordinal years then the number of ordinal months then the number of ordinal days that consume those cardinal days, in that order. We cannot count ordinal months until we have consumed all the ordinal years and cannot count ordinal days until we have consumed all the ordinal months.



So Jure’s answer is the correct one. 25 years, 6 months, and 1 day.



Regards,

Ricardo

  1. Helpful
  1. Michael Kramer
  2. Monday, 2 December 2019 11:10 AM UTC
However, if I add first 25 years; then 5 months; then 4 weeks; and finally 1 day - - I still end up at 01-Mar-2019. And if I don't want weeks included I convert at 7 days/week.

Problem I see is counting ordinal months when you end up at invalid date: 31-Feb-2019 is not a valid date. What is the correct, valid date? And is there same calendar period between 28-Jan-2019 - - 28-Feb-2019 as there is from 31-Jan-2019 (3-days later) - - 28-Feb-2019.
  1. Helpful
  1. Michael Kramer
  2. Monday, 2 December 2019 11:19 AM UTC
I accept that month of Feb-2019 is just 28 days I can also convert those 28-days to 1 month - however, than same calculation makes 04-Jan => 01-Feb equal to one month also - and that just doesn't look right to me because 1 month out from 04-Jan seems to be 04-Feb. It is the end-of-month discussion when months have different length that is difficult in my view.
  1. Helpful
There are no comments made yet.
Michael Kramer Accepted Answer Pending Moderation
  1. Sunday, 1 December 2019 20:08 PM UTC
  2. PowerBuilder
  3. # 5

Hi Jure, my attempt:

Action Added Result
Initial date - none - 31-Aug-1993
Add 25 years 25Y 31-Aug-2018
Add 5 months 25Y, 5M 31-Jan-2019
Add 28 days 25Y, 5M, 28D 28-Feb-2019
Add 1 more day 25Y, 5M, 29D 01-Mar-2019

So my answer is 25 years, 5 months, and 29 days.

HTH /Michael

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.