Reputation: 6051
I'm trying to get a date and a hour from the user.
date = raw_input("enter date (example: 25/12/1990) ")
hour = raw_input("enter hour (example: 20:00) ")
That is how I have done that, but I think this is not a good way to implement it.
After the input I would like to compare the inputted time to the current time, and to other times.
I have planned to use the time
module of python.
What would you suggest?
Upvotes: 0
Views: 396
Reputation: 15305
The above gives you a date as a string. You'll need to convert that string into a datetime object, and handle any errors which arise. If you restrict your users to simple dates then you can do everything with the datetime module, like this:
>>> import datetime
>>> date = "15/03/2012"
>>> datetime.datetime.strptime(date, "%d/%m/%Y")
datetime.datetime(2012, 3, 15, 0, 0)
>>> hour = "13:01"
>>> datetime.datetime.strptime(hour, "%H:%M")
datetime.datetime(1900, 1, 1, 13, 1)
>>> datetime.datetime.strptime(date + " " + hour, "%d/%m/%Y %H:%M")
datetime.datetime(2012, 3, 15, 13, 1)
>>> date = "1/4/2012"
>>> hour = "3:01"
>>> datetime.datetime.strptime(date + " " + hour, "%d/%m/%Y %H:%M")
datetime.datetime(2012, 4, 1, 3, 1)
You can use other datetime module code to do things like get the current date and time, or to find difference between two dates or times.
Upvotes: 1