#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    What's wrong with this code?


    I can't for the life of me figure out what's wrong with this code.
    Code:
            if time.strftime("%H") == '00':
                mytime = "12:"+time.strftime("%M")+":"+time.strftime("%S")+"AM"
            elif time.strftime("%H") == "01":
                mytime = "1:"+time.strftime("%M")+":"+time.strftime("%S")+"AM"
            elif time.strftime("%H") == "02":
                mytime = "2:"+time.strftime("%M")+":"+time.strftime("%S")+"AM"
            elif time.strftime("%H") == "03":
                mytime = "3:"+time.strftime("%M")+":"+time.strftime("%S")+"AM"
            elif time.strftime("%H") == "04":
                mytime = "4:"+time.strftime("%M")+":"+time.strftime("%S")+"AM"
            elif time.strftime("%H") == "05":
                mytime = "5:"+time.strftime("%M")+":"+time.strftime("%S")+"AM"
            elif time.strftime("%H") == "06":
                mytime = "6:"+time.strftime("%M")+":"+time.strftime("%S")+"AM"
            elif time.strftime("%H") == "07":
                mytime = "7:"+time.strftime("%M")+":"+time.strftime("%S")+"AM"
            elif time.strftime("%H") == "08":
                mytime = "8:"+time.strftime("%M")+":"+time.strftime("%S")+"AM"
            elif time.strftime("%H") == "09":
                mytime = "9:"+time.strftime("%M")+":"+time.strftime("%S")+"AM"
            elif time.strftime("%H") == "10":
                mytime = "10:"+time.strftime("%M")+":"+time.strftime("%S")+"AM"
            elif time.strftime("%H") == "11":
                mytime = "11:"+time.strftime("%M")+":"+time.strftime("%S")+"AM"
            elif time.strftime("%H") == "12":
                mytime = "12:"+time.strftime("%M")+":"+time.strftime("%S")+"PM"
            elif time.strftime("%H") == "13":
                mytime = "1:"+time.strftime("%M")+":"+time.strftime("%S")+"PM"
            elif time.strftime("%H") == "14":
                mytime = "2:"+time.strftime("%M")+":"+time.strftime("%S")+"PM"
            elif time.strftime("%H") == "15":
                mytime = "3:"+time.strftime("%M")+":"+time.strftime("%S")+"PM"
            elif time.strftime("%H") == "16":
                mytime = "4:"+time.strftime("%M")+":"+time.strftime("%S")+"PM"
            elif time.strftime("%H") == "17":
                mytime = "5:"+time.strftime("%M")+":"+time.strftime("%S")+"PM"
            elif time.strftime("%H") == "18":
                mytime = "6:"+time.strftime("%M")+":"+time.strftime("%S")+"PM"
            elif time.strftime("%H") == "19":
                mytime = "7:"+time.strftime("%M")+":"+time.strftime("%S")+"PM"
            elif time.strftime("%H") == "20":
                mytime = "8:"+time.strftime("%M")+":"+time.strftime("%S")+"PM"
            elif time.strftime("%H") == "21":
                mytime = "9:"+time.strftime("%M")+":"+time.strftime("%S")+"PM"
            elif time.strftime("%H") == "22":
                mytime = "10:"+time.strftime("%M")+":"+time.strftime("%S")+"PM"
            elif time.strftime("%H") == "23":
                mytime = "11:"+time.strftime("%M")+":"+time.strftime("%S")+"PM"
            else:
                pass
    I get the following error,
    AttributeError: 'str' object has no attribute 'strftime'
    I used import time, is there another import I should use? Or is something else wrong?
    Update: I removed that code from the area where I was using it, I don't need help with it any more.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    Germany
    Posts
    11
    Rep Power
    0
    there's nothing wrong with this code, the error must be somewhere else;
    i guess you do str.strftime() somewhere...
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    Glad you got it resolved. Just so you know, strftime can produce a perfectly workable AM/PM prefix.
    Code:
    import time
    mytime = time.strftime("%I:%M:%S %p")
    print mytime
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    just one question what is the "%I" and "%p"
    please explain
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    %I is a placeholder for the current hour as shown on a 12 hour clock; %H does the same thing but for a 24 hour clock. %p on the other hand is slightly different and is replaced with either AM or PM, depending on the time of day .

    You can find the full list of formatting characters available here: http://www.python.org/doc/2.4/lib/module-time.html

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Thanks netytan.

IMN logo majestic logo threadwatch logo seochat tools logo