如何根据用户输入制作日期时间对象?
How to make a datetime object from user input?
我正在尝试制作一个闹钟。如何根据用户输入创建日期时间对象,其中用户输入分别为小时、分钟和秒。例如:
from datetime import datetime
# user-input: at 2:30
hr = "2"
m = "30"
s = "0"
from datetime import datetime
def GetDate():
isValid=False
while not isValid:
userIn = input("Type Date dd/mm/yy: ")
try: # strptime throws an exception if the input doesn't match the pattern
d = datetime.datetime.strptime(userIn, "%d/%m/%y")
isValid=True
except:
print("Invalid")
return d
#test the function
print(GetDate())
我正在尝试制作一个闹钟。如何根据用户输入创建日期时间对象,其中用户输入分别为小时、分钟和秒。例如:
from datetime import datetime
# user-input: at 2:30
hr = "2"
m = "30"
s = "0"
from datetime import datetime
def GetDate():
isValid=False
while not isValid:
userIn = input("Type Date dd/mm/yy: ")
try: # strptime throws an exception if the input doesn't match the pattern
d = datetime.datetime.strptime(userIn, "%d/%m/%y")
isValid=True
except:
print("Invalid")
return d
#test the function
print(GetDate())