Python DateTime Manipulation

Chris Rebert clp2 at rebertia.com
Thu Jan 15 17:25:20 EST 2009


On Thu, Jan 15, 2009 at 2:19 PM, Kingston <kingstonlee at gmail.com> wrote:
> I have a user input a date and time as a string that looks like:
> "200901010100" but I want to do a manipulation where I subtract 7 days
> from it.
>
> The first thing I tried was to turn the string into a time with the
> format "%Y%m%d%H%M" and then strip out the day value, turn it into an
> int and subtract seven, but the value "-6" doesn't mean anything in
> terms of days =).
>
> Does anyone have an idea as to how I can subtract 7 days without
> turning the date string into an int?

>>> from datetime import datetime, timedelta
>>> now = datetime.now()
>>> now
datetime.datetime(2009, 1, 15, 14, 22, 56, 416662)
>>> week = timedelta(days=7)
>>> now - week
datetime.datetime(2009, 1, 8, 14, 22, 56, 416662)


Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list