Thread: Valid Year

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    101
    Rep Power
    11

    Valid Year


    Hi, I am trying to find out how to check if it's a valid year or not i.e. above 1950 and not greater than the current. I have a string in which the first four characters need to be a valid Year. So I am extracting the first four characters and now I need see if all the characters are valid digits and then check for the validity of the year. Any built-in functions I can use or any ideas thanks for help.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    Code:
    import time
    
    def valid_date(s):
        dtnow = time.localtime()
        year = dtnow[0]
        try:
            y = int(s)
            if y > 1950 and y < year:
                return 1
        except ValueError:
            pass
        return 0        
    
    print valid_date("1984")
    print valid_date("abc")
    print valid_date("2007")
    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
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You might want to look at the strftime() and strptime() functions in the time module - i've had alot of fun playing with date formats like this before. Anyway these two are definatly good for comparing/playign with times!

    http://www.python.org/doc/2.3.3/lib/module-time.html

    Take care,

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


IMN logo majestic logo threadwatch logo seochat tools logo