|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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 Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month |
|
#3
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Valid Year |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|