[Tutor] datetime.timedelta Output Format
Kent Johnson
kent37 at tds.net
Mon Apr 2 12:34:01 CEST 2007
William Allison wrote:
> Is there a way to have the output of "print tis" in the same format as
> "print now" and "print tafmsd" in the code below?
> Thanks,
> Will
>
>
> savage:~ wallison$ python
> Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
> [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import datetime
> >>> now = datetime.date.today()
> >>> print now
> 2007-04-01
> >>> tafmsd = datetime.date(1994, 2, 23)
> >>> print tafmsd
> 1994-02-23
> >>> tis = now - tafmsd
> >>> print tis
> 4785 days, 0:00:00
It looks like Gustavo Niemeyer's dateutil module will at least do the
year/month/day calculation if not the formatting:
In [1]: from datetime import date
In [2]: from dateutil import relativedelta
In [3]: now = date.today()
In [9]: rd = relativedelta.relativedelta(now, tafmsd)
In [10]: rd
Out[10]: relativedelta(years=+13, months=+1, days=+10)
http://labix.org/python-dateutil
Kent
More information about the Tutor
mailing list