Re: [Python-Dev] How do you get yesterday from a time object

Discussion in 'Python' started by Simon Brunning, Apr 19, 2005.

  1. On 4/19/05, Ralph Hilton <> wrote:
    > i'm a beginning python programmer.
    >
    > I want to get the date for yesterday
    >
    > nowTime = time.localtime(time.time())
    > print nowTime.
    > oneDay = 60*60*24 # number seconds in a day
    > yday = nowTime - oneDay # <-- generates an error
    > print yday.strftime("%Y-%m-%d")


    today = datetime.date.today()
    previous_working = today - datetime.timedelta(days=1)

    --
    Cheers,
    Simon B,
    ,
    http://www.brunningonline.net/simon/blog/
     
    Simon Brunning, Apr 19, 2005
    #1
    1. Advertising

  2. Simon Brunning

    Larry Bates Guest

    Use datetime module if you are on 2.3 or 2.4, otherwise you
    can do:

    today=time.time()
    yesterday=today-24*60*60 # 24 hours x 60 minutes x 60 seconds

    Your problem is that time.localtime() converts to 9 value
    tuple (you can't subtract seconds from a tuple). You do
    the math on today and convert using time.localtime after.

    For future reference: Please post a copy of your traceback
    instead of just saying "<-- generates and error". It helps
    people to respond more quickly and accurately.

    -Larry Bates


    Simon Brunning wrote:
    > On 4/19/05, Ralph Hilton <> wrote:
    >
    >>i'm a beginning python programmer.
    >>
    >>I want to get the date for yesterday
    >>
    >>nowTime = time.localtime(time.time())
    >>print nowTime.
    >>oneDay = 60*60*24 # number seconds in a day
    >>yday = nowTime - oneDay # <-- generates an error
    >>print yday.strftime("%Y-%m-%d")

    >
    >
    > today = datetime.date.today()
    > previous_working = today - datetime.timedelta(days=1)
    >
     
    Larry Bates, Apr 19, 2005
    #2
    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. Mickey Segal
    Replies:
    4
    Views:
    529
    Mickey Segal
    Apr 15, 2006
  2. -vr_stupids

    get "yesterday's" date in iso format

    -vr_stupids, Jan 23, 2004, in forum: Python
    Replies:
    4
    Views:
    4,213
    -vr_stupids
    Jan 24, 2004
  3. Thierry Lam

    Finding yesterday's date with datetime

    Thierry Lam, May 15, 2006, in forum: Python
    Replies:
    2
    Views:
    523
  4. Xiahui
    Replies:
    3
    Views:
    270
    Mike Wahler
    Mar 28, 2006
  5. Marcin Jurczuk

    Time.yesterday ? :)

    Marcin Jurczuk, Nov 17, 2005, in forum: Ruby
    Replies:
    13
    Views:
    364
    Adam Sanderson
    Nov 18, 2005
Loading...

Share This Page