[Python] How to get yesterday's date

Is there an existing function or "conversion" that would allow for getting yesterday's date with ...time...???

Thank you
Configuration: Windows XP Firefox 1.5.0.9

4 answers

  1. Contributor
    Much simpler :)

     import datetime print "today:", datetime.date.today() print "yesterday:", datetime.date.today()-datetime.timedelta(1) 

    --
    Without politeness, we would only come together to fight.
    We must therefore either live alone or be polite.
    Alphonse Karr
    2
    1. Ça fonctionne parfaitement !!

      Merci pour les conseils

      Eclipsse
      0
    2. Hey,
      so how do you indicate the date from one month ago?
      0
    3. Moderator
      @getnateUse timedelta (see the Python documentation).
      0
  2. Contributor
    Hello
    You are -1 on today's date :)
    --
    Without politeness, we would only meet to fight.
    So one must either live alone or be polite.
    Alphonse Karr
    0
    1. It's not that simple if you want a date in the format "20070702"!
      --------------------------------------------------------------------------------

      today = time()

      # 86400 = 24 h x 3600 seconds => 1 day in seconds!
      yesterday = today - 86400
      formatted = gmtime(yesterday)

      if len(str(formatted[1]))<2:
      first = "0" + str(formatted[1])
      else:
      first = str(formatted[1])
      if len(str(formatted[2]))<2:
      second = "0" + str(formatted[2])
      else:
      second = str(formatted[2])

      concat = str(formatted[0]) + first + second

      === > because otherwise it could result in "200772"!!!
      0
      1. Moderator
        If you want a date in the format "20070702"!

        See strftime()

        --
        “La vie est courte - Vous avez besoin de Python” -- Bruce Eckel, member of the ANSI C++ committee
        0