How to find difference in years between two dates?

B

Bruno Desthuilliers

thebjorn said:
All input routines, whether they're from a web-form, database, command
line, or anywhere else, only produce objects from the datetime module
for calendar data.

Seems quite sensible.
That way the program logic doesn't have to guess
which format it's getting... I suppose I could do something like:

def age(born):
mx_born = mx.DateTime.Date(born.year, born.month, born.day)
...

Yes, I was thinking of something like this.
 
T

thebjorn

John said:
Jan 31 to Feb 27: 27d (ex) 28d (in)
Jan 31 to Feb 28: 28d (ex) 1m 1d (in)
Jan 31 to Mar 01: 1m 1d (ex) 1m 2d (in)
So 1 day short of 1m 1d is not 1m 0 d???

Exactly. Just as a person born on 1999-3-1 isn't a year old on
2000-2-29. Perfectly regular, consistent and reasonable.
I'd call this unreasonable, inconsistent, anomalous -- especially
when on the same website you do 1993-01-31 plus 1 month, it
gives you 1993-02-28 (as I'd expect).

You're entitled to your opinion.

-- bjorn
 
J

John Machin

thebjorn said:
Exactly. Just as a person born on 1999-3-1 isn't a year old on
2000-2-29. Perfectly regular, consistent and reasonable.

Is a person born on 1993-08-01 a year old on 1994--07-31?

I don't understand. The examples that I showed went from the last day
of a month to the last day of another month. You justify a manifest
inconsistency in the results by reference to an example that goes from
the first day of a month to the last day of a month??
You're entitled to your opinion.

And you to yours :)
 
T

thebjorn

John said:
I don't understand. The examples that I showed went from the last day
of a month to the last day of another month. [...]

Q1: is ((date-4days)+4days) == date?
Q2: is (((date-4days)+1month)+4days) == date+1month?

Ok, let's use Python'ish syntax (including numbering the days from 0
upwards, and backwards from -1, the last day of the month), you want
the last day of a month plus a month be the last day of the next
month. Simplistically, something like:

month[-1] + 1 month == (month+1)[-1] {last-to-last}

but that's obviously not the entire rule you want, unless 4-30 + 1
month == 5-31? So you would also like to have:

month + 1 month = (month+1) {lock-step}

we'd like yesterday to be a day ago? So for suitable i:

month - 1 day == month[i-1] {yesterday-1}
month[0] - 1 day == (month-1)[-1] {yesterday-2}

which leads to a natural definition for when tomorrow is:

month + 1 day == month[i+1] {tomorrow-1}
month[-1] + 1 day == (month+1)[0] {tomorrow-2}

So far so good. Now let's count backwards:

month[-1] - 1 day == month[-2] <by: yesterday-1>
month[-2] - 1 day == month[-3] <by: yesterday-1>
month[-3] - 1 day == month[-4] <by: yesterday-1>
etc.

In other words, if you insist that the last day of the month is a well
defined concept and you want a day ago to be yesterday then month[-4],
the forth-to-last day of the month, is necessarily also well
defined. Having a well defined month, I'll apply your rules for
adding a month:

month[-4] + 1 month == (month+1)[-4] <by: last-to-last>

but you don't like this, because that means that e.g.:

april[-4] + 1 month == may[-4]
april[27] + 1 month == may[28]

which in addition to {lock-step}:

april[27] + 1 month == may[27]

either gives an inconsistent, ill-formed, or FUZZY system (although
I would call it "regular" ;-)

My approach is simpler since it doesn't define addition, only
subtraction on valid dates, so if I switch to representing dates as
(month, day):

(a, b) - (c, d) := a - c iff b < d {subtract}
else a - c - 1

{subtract} is irregular but well defined for all valid dates (it will
always give you an answer, and it's always the same answer ;-) :

(2,29) - (1,31) == 0
(3,1) - (1,31) == 2

I can add "day addition" and still be ok:

(m,d) + 1 day := (m,d+1) {tomorrow-1}
(m,-1) + 1 day := (m+1,0) {tomorrow-2}
(m,d) - 1 day := (m,d-1) {yesterday-1}
(m,0) - 1 day := (m-1,-1) {yesterday-2}

Now my system is well-formed and consitent, even though it is
irregular, and it will answer yes to Q1 above. I can't see a way of
adding month addition to this and stay consistent without enumerating
special cases for every month, so Q2 can't even be asked in my system.
And you to yours :)

Ok, I've explained why I hold mine... You care to do the same?

-- bjorn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top