Newbie: Subtracting Dates

mowestusa justnotworking at mail.com
Thu Dec 12 15:30:52 EST 2002


Paul,
Thanks for your comments.  You had some interesting thoughts.  Your right
the example that I gave wouldn't tell you that much if you were working with
dates that were less then a year apart.  Also the format that I suggest
doesn't really account for the leap year and other mathamatical problems.

This all started because I have an associate who likes to express a person's
age in that way to the month and day.  So I thought it would be fun to write
a python script that would do the math automatically.  I suppose you are
write though.  If you really want to be exact then a different way of
measuring the time needs to be used.

Thanks for your thoughts.

mowestusa

----- Original Message -----
From: "Paul Rubin" <phr-n2002b at NOSPAMnightsong.com>
Newsgroups: comp.lang.python
Sent: Sunday, December 08, 2002 2:30 AM
Subject: Re: Newbie: Subtracting Dates


> "mowestusa" <justnotworking at mail.com> writes:
> > Basically, I would like to create a program that subtracts Date #1 from
Date
> > #2.  The output would give the total number of "years", "months", and
"days"
> > in between dates.  Like the following example:
> >
> > Date #1    1/1/1900
> > Date #2    12/6/2002
> > Years:    102
> > Months:  11
> > Days:        5
> >
> > If we are talking about antiques and Date #1 was the produced date.  The
> > piece would be 102 years, 11 months, and 5 days.
> >
> > Thanks for any direction.
>
> Well, what does "102 years, 11 months, and 5 days" really mean?
>
> For example, if date #1 is July 4, 1976 and date #2 is July 4, 1977,
> what do you want the output to be?  Probably 1 year, 0 months, 0 days, ok?
> The two dates are 365 days apart, so what else can you want?
>
> Now what if date #1 is July 4, 1975 and date #2 is July 4, 1976?  This
> time, because 1976 is a leap year, the dates are 366 days apart
> instead of 365.  So is the answer still 1 year, 0 months, and 0 days,
> in which case that answer doesn't always mean the same thing
> (basically it's not in the same units as the quantities being
> subtracted)?  Or is it 1 year, 0 months, and 1 day?  In that case your
> 1/1/1900 example isn't right, because of the 25 leap days in the
> interval (1900 is not a leap year).
>
> Maybe you want to just use the time.mktime function to convert both
> dates into absolute time (seconds since epoch) and then subtract
> arithmetically.





More information about the Python-list mailing list