Calculating future dates

Discussion in 'Python' started by Toine, Feb 2, 2007.

  1. Toine

    Toine Guest

    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
     
    Toine, Feb 2, 2007
    #1
    1. Advertising

  2. Toine

    Dan Bishop Guest

    On Feb 1, 6:51 pm, "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.
    >
    > Thanks


    str(datetime.date.today() + datetime.timedelta(31))
     
    Dan Bishop, Feb 2, 2007
    #2
    1. Advertising

  3. 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
     
    Irmen de Jong, Feb 2, 2007
    #3
  4. Toine

    Toine Guest

    On Feb 1, 4:54 pm, "Dan Bishop" <> wrote:
    > On Feb 1, 6:51 pm, "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.

    >
    > > 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))

    Thanks for your help
     
    Toine, Feb 2, 2007
    #4
  5. Toine

    Ben Finney Guest

    "Toine" <> writes:

    > On Feb 1, 4:54 pm, "Dan Bishop" <> 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
     
    Ben Finney, Feb 2, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    904
    Rob Schieber
    Sep 30, 2005
  2. celsius
    Replies:
    2
    Views:
    519
    Stan Milam
    Jan 5, 2005
  3. clintonb
    Replies:
    9
    Views:
    933
    James Kanze
    May 23, 2007
  4. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    230
    Mark Schupp
    Aug 9, 2004
  5. Toine

    Calculating a future date

    Toine, Feb 6, 2007, in forum: Ruby
    Replies:
    6
    Views:
    153
    Rob Biedenharn
    Feb 8, 2007
Loading...

Share This Page