Python 使用变量添加时间
Python add time using variables
我想使用变量将小时和分钟添加到时间中。因此,在下面的示例中,我希望将 3 和 30 存储在变量中,而不是 'hours=3, minutes=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)
谢谢。
使用命令行你可以像下面这样写
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
我想使用变量将小时和分钟添加到时间中。因此,在下面的示例中,我希望将 3 和 30 存储在变量中,而不是 'hours=3, minutes=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)
谢谢。
使用命令行你可以像下面这样写
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