I want to be able to implement a condition in my program where it would only run for N number of hours, maybe the user could specify N, but let's not jump ahead.
I figured I could use datetime.now() and store the value below in a variable, time
>>> time
>>> time = datetime.datetime(2013, 12, 9, 21, 50, 32, 405329)
Any ideas on how I can access the fourth field between the (--), seeing as it's a string?
My condition would be something like while time != timeEnd where timeEnd would be the value of below:
>>> timeEnd = datetime.datetime(2013, 12, 9, 21+N, 50, 32, 405329)
Thanks
解决方案
You can add a timedelta to a datetime to get a new datetime.
>>> import datetime
>>> now = datetime.datetime.now()
>>> end_time = now + datetime.timedelta(hours=6)
>>> print now
2013-12-09 17:03:06.979628
>>> print end_time
2013-12-09 23:03:06.979628
>>>
Then...
while datetime.datetime.now() < end_time:
pass # do work