Velocity Reviews > Calculating future dates

# Calculating future dates

Toine
Guest
Posts: n/a

 02-02-2007
Hello,

I'm new to Python so please bare with me...

I need to calculate a date that is exactly 31 days from the current
date in YYYY-MM-DD format. I know that date.today() returns the
current date, but how can I add 31 days to this result? I'm sure this
task is simple, but I haven't been able to figure it out.

Thanks

Dan Bishop
Guest
Posts: n/a

 02-02-2007
On Feb 1, 6:51 pm, "Toine" <(E-Mail Removed)> wrote:
> Hello,
>
> I'm new to Python so please bare with me...
>
> I need to calculate a date that is exactly 31 days from the current
> date in YYYY-MM-DD format. I know that date.today() returns the
> current date, but how can I add 31 days to this result? I'm sure this
> task is simple, but I haven't been able to figure it out.
>
> Thanks

str(datetime.date.today() + datetime.timedelta(31))

Irmen de Jong
Guest
Posts: n/a

 02-02-2007
Toine wrote:
> Hello,
>
> I'm new to Python so please bare with me...
>
> I need to calculate a date that is exactly 31 days from the current
> date in YYYY-MM-DD format. I know that date.today() returns the
> current date, but how can I add 31 days to this result? I'm sure this
> task is simple, but I haven't been able to figure it out.

>>> import datetime
>>> print datetime.date.today()+datetime.timedelta(days=31)

2007-03-05
>>>

---Irmen

Toine
Guest
Posts: n/a

 02-02-2007
On Feb 1, 4:54 pm, "Dan Bishop" <(E-Mail Removed)> wrote:
> On Feb 1, 6:51 pm, "Toine" <(E-Mail Removed)> wrote:
>
> > Hello,

>
> > I'm new to Python so please bare with me...

>
> > I need to calculate a date that is exactly 31 days from the current
> > date in YYYY-MM-DD format. I know that date.today() returns the
> > current date, but how can I add 31 days to this result? I'm sure this
> > task is simple, but I haven't been able to figure it out.

>
> > Thanks

>
> str(datetime.date.today() + datetime.timedelta(31))

Your example gave me a few errors but I was able to adapt it into
this:

str(date.today() + timedelta(31))

Ben Finney
Guest
Posts: n/a

 02-02-2007
"Toine" <(E-Mail Removed)> writes:

> On Feb 1, 4:54 pm, "Dan Bishop" <(E-Mail Removed)> wrote:
> > str(datetime.date.today() + datetime.timedelta(31))

>
> Your example gave me a few errors but I was able to adapt it into
> this:
>
> str(date.today() + timedelta(31))

That only works if you're importing 'date' and 'timedelta' into the
current namespace. It's better to keep them in the 'datetime'
namespace, so it's clear what comes from where.

>>> import datetime
>>> str(datetime.date.today() + datetime.timedelta(31))

--
\ "How many people here have telekenetic powers? Raise my hand." |
`\ -- Emo Philips |
_o__) |
Ben Finney

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post clintonb C++ 9 05-23-2007 09:32 AM David Lozzi ASP .Net 1 09-30-2005 02:18 PM celsius C Programming 2 01-05-2005 08:02 AM PW ASP General 4 08-09-2004 04:42 PM Joe Black NZ Computing 17 10-08-2003 11:44 PM