我想使用变量添加小时和分钟。因此,在下面的示例中,我希望将3和30存储在变量中,而不是让'小时= 3,分钟= 30',我希望将3和30存储在变量中。
这可能吗?
import datetime
now = datetime.datetime.now()
ahead_time = now + datetime.timedelta(hours=3,minutes=30)
print(" now time is ", now, " ahead_time is ", ahead_time)
感谢。
答案 0 :(得分:0)
通过使用命令行,您可以写如下
import datetime
import argparse
def getTime(hour, minutes):
now = datetime.datetime.now()
ahead_time = now + datetime.timedelta(hours=int(hour),minutes=int(minutes))
return " now time is %s ahead_time is %s" %(now, ahead_time)
def parser():
parser = argparse.ArgumentParser(description='Process the input time.')
parser.add_argument('--hour', dest='hour')
parser.add_argument('--minutes', dest='minutes')
return vars(parser.parse_args())
if __name__ == "__main__":
args = parser()
hour = args.get('hour', None)
minutes = args.get('minutes', None)
if hour and minutes:
print getTime(hour, minutes)
用法:
$ python test.py --hour 3 --minutes 30
now time is 2017-03-23 06:37:49.890000 ahead_time is 2017-03-23 10:07:49.890000