Newbie: Subtracting Dates
Paul Rubin
phr-n2002b at NOSPAMnightsong.com
Sun Dec 8 03:30:08 EST 2002
"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