在python中将日期设置为第二天的日期

时间:2018-02-22 01:03:14

标签: python python-2.7

我正在处理我的python代码以生成日期格式,以便我能够将数据搜索到数据库中,例如:20180222010000

我目前使用的代码遇到了一些麻烦,因为当我运行代码时,它应该将日期添加到第二天将self.program_day设置为{{1}当我的当前时间显示在21:00到06:00之间时,来自1。在我的代码上,它只会显示同一天的日期,例如:今天的日期是22,所以如果我将0设置为1,那么它仍然会显示今天的日期22,它应该是23.如果我想设置日期到第二天,我将不得不再次激活我的代码,这样我就可以将每天的日期添加到24,25,26等等,之后我必须运行我的代码两次。

以下是代码:

self.program_day

这是program_start_date:

def get_next_day(self, time_string):
    to_datetime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(time_string, '%Y%m%d')))
    nextday = to_datetime + datetime.timedelta(days = self.program_day)
    return nextday.strftime('%Y%m%d')

next_day = time.strftime("%Y%m%d")
next_day = self.get_next_day(next_day)
get_minutes = time.strftime("%M")
next_day = str(next_day)
program_start_date = ''

if self.program_day == 5 and int(get_hour) >= 20 and int(get_hour) <= 23:
   pass
else:
   self.program_day += 1

for channels in channels_list:
    if str(get_hour) == '00':
       program_start_date = str(next_day + '00' + get_minutes + '00')
    elif str(get_hour) == '01':
       program_start_date = str(next_day + '01' + get_minutes + '00')
    elif str(get_hour) == '02':
       program_start_date = str(next_day + '02' + get_minutes + '00')
    elif str(get_hour) == '03':
       program_start_date = str(next_day + '03' + get_minutes + '00')
    elif str(get_hour) == '04':
        program_start_date = str(next_day + '04' + get_minutes + '00')
    elif str(get_hour) == '05':
       program_start_date = str(next_day + '05' + get_minutes + '00')
    elif str(get_hour) == '06':
       program_start_date = str(next_day + '06' + get_minutes + '00')
    elif str(get_hour) == '07':
       program_start_date = str(next_day + '07' + get_minutes + '00')
    elif str(get_hour) == '08':
       program_start_date = str(next_day + '08' + get_minutes + '00')
    elif str(get_hour) == '09':
       program_start_date = str(next_day + '09' + get_minutes + '00')
    elif str(get_hour) == '10':
       program_start_date = str(next_day + '10' + get_minutes + '00')
    elif str(get_hour) == '11':
       program_start_date = str(next_day + '11' + get_minutes + '00')
    elif str(get_hour) == '12':
       program_start_date = str(next_day + '12' + get_minutes + '00')
    elif str(get_hour) == '13':
       program_start_date = str(next_day + '13' + get_minutes + '00')
    elif str(get_hour) == '14':
       program_start_date = str(next_day + '14' + get_minutes + '00')
    elif str(get_hour) == '15':
       program_start_date = str(next_day + '15' + get_minutes + '00')
    elif str(get_hour) == '16':
       program_start_date = str(next_day + '16' + get_minutes + '00')
    elif str(get_hour) == '17':
       program_start_date = str(next_day + '17' + get_minutes + '00')
    elif str(get_hour) == '18':
       program_start_date = str(next_day + '18' + get_minutes + '00')
    elif str(get_hour) == '19':
       program_start_date = str(next_day + '19' + get_minutes + '00')
    elif str(get_hour) == '20':
       program_start_date = str(next_day + '20' + get_minutes + '00')
    elif str(get_hour) == '21':
       program_start_date = str(next_day + '21' + get_minutes + '00')
    elif str(get_hour) == '22':
       program_start_date = str(next_day + '22' + get_minutes + '00')
    elif str(get_hour) == '23':
       program_start_date = str(next_day + '23' + get_minutes + '00')

    print "program_start_date for......................next_day"
    print program_start_date

我想要实现的是我想将program_start_date for......................next_day 20180222003000 program_start_date for......................next_day 20180222003000 program_start_date for......................next_day 20180222003000 program_start_date for......................next_day 20180222003000 program_start_date for......................next_day 20180222003000 program_start_date for......................next_day 20180222003000 program_start_date for......................next_day 20180222003000 日期设置为类似next_day的内容。我必须使用if语句,因此我可以设置日期,时间和分钟,使其显示为20180223

您能否向我展示一个示例,当我使用20180222010000变量时,我可以设置为第二天的日期?

1 个答案:

答案 0 :(得分:0)

from datetime import datetime
from dateutil.relativedelta import relativedelta

date_now = datetime.now().date()
start_date = date_now + relativedelta(days=+1)

这将是成功的前提。我在python3中进行了测试。

希望它对您有帮助