在python中更改为上一天的日期

时间:2017-08-31 16:03:56

标签: python string python-2.7 datetime

我正在处理我的python脚本以从按钮对象中获取字符串,因此我可以使用它来设置日期格式,以及从字符串中获取的时间以将其存储在列表中。当我从按钮对象中获取字符串时,我想为每个字符串设置日期,例如:29/08/2017 11:30PM30/08/2017 12:00AM30/08/2017 12:30AM

当我尝试这个时:

if day_date >= 0 and day_date <= 6:
   if epg_time3 == '12:00AM':
      if day_date > 0:  
         currentday = datetime.datetime.now() + datetime.timedelta(days = 0)
         nextday = datetime.datetime.now() + datetime.timedelta(days = self.program_day)

         if currentday != nextday:
            epg_time_1 = datetime.datetime.now() + datetime.timedelta(days = self.program_day + 1)
            epg_time_2 = datetime.datetime.now() + datetime.timedelta(days = self.program_day + 1)
            epg_time_3 = datetime.datetime.now() + datetime.timedelta(days = self.program_day + 1)
        elif currentday == nextday:
             epg_time_1 = datetime.datetime.now() + datetime.timedelta(days = self.program_day)
             epg_time_2 = datetime.datetime.now() + datetime.timedelta(days = self.program_day - 1)
             epg_time_3 = datetime.datetime.now() + datetime.timedelta(days = self.program_day - 1)

它将显示输出:

self.epg_time_1
['29/08/2017 11:00PM']
self.epg_time_2
['29/08/2017 11:30PM']
self.epg_time_3
['29/08/2017 12:00AM']

当我再次调用EPG_Times函数时,它将显示如下输出:

self.epg_time_1
['30/08/2017 11:00PM']
self.epg_time_2
['30/08/2017 11:30PM']
self.epg_time_3
['30/08/2017 12:00AM']

应该是:

self.epg_time_1
['30/08/2017 11:00PM']
self.epg_time_2
['30/08/2017 11:30PM']
self.epg_time_3
['31/08/2017 12:00AM']

您可以在第二天看到时间12:00AM,因此我想将其设置为31而不是30。我已从days = self.program_day + 1更改为days = self.program_day - 1,但当字符串显示变量11:00PM中的11:30PM12:00AMepg_time_1时,{{1 }和epg_time_2,它会显示如下输出:

epg_time_3

以下是完整代码:

self.epg_time_1
['30/08/2017 11:00PM']
self.epg_time_2
['30/08/2017 11:30PM']
self.epg_time_3
['30/08/2017 12:00AM']

我所期望的代码是在我调用EPG_time(self)函数时更改为从按钮对象获取的每个字符串的前一天日期。如果self.program_day = list() def EPG_Times(self): self.epg_time_1 = list() self.epg_time_2 = list() self.epg_time_3 = list() epg_time1 = str(self.getControl(344).getLabel()) epg_time2 = str(self.getControl(345).getLabel()) epg_time3 = str(self.getControl(346).getLabel()) day_date = self.program_day day = '' month = '' year = '' if day_date >= 0 and day_date <= 6: if epg_time3 == '12:00AM': if day_date == 0: if epg_time1 == '12:00AM': epg_time_1 = datetime.datetime.now() + datetime.timedelta(days = self.program_day + 1) epg_time_2 = datetime.datetime.now() + datetime.timedelta(days = self.program_day + 1) epg_time_3 = datetime.datetime.now() + datetime.timedelta(days = self.program_day + 1) else: epg_time_1 = datetime.datetime.now() + datetime.timedelta(days = self.program_day) epg_time_2 = datetime.datetime.now() + datetime.timedelta(days = self.program_day) epg_time_3 = datetime.datetime.now() + datetime.timedelta(days = self.program_day + 1) else: currentday = datetime.datetime.now() + datetime.timedelta(days = 0) nextday = datetime.datetime.now() + datetime.timedelta(days = self.program_day) if currentday != nextday: epg_time_1 = datetime.datetime.now() + datetime.timedelta(days = self.program_day + 1) epg_time_2 = datetime.datetime.now() + datetime.timedelta(days = self.program_day + 1) epg_time_3 = datetime.datetime.now() + datetime.timedelta(days = self.program_day + 1) elif currentday == nextday: epg_time_1 = datetime.datetime.now() + datetime.timedelta(days = self.program_day) epg_time_2 = datetime.datetime.now() + datetime.timedelta(days = self.program_day - 1) epg_time_3 = datetime.datetime.now() + datetime.timedelta(days = self.program_day - 1) epg1_day = epg_time_1.strftime("%d") epg1_month = epg_time_1.strftime("%m") epg1_year = epg_time_1.strftime("%Y") epg2_day = epg_time_2.strftime("%d") epg2_month = epg_time_2.strftime("%m") epg2_year = epg_time_2.strftime("%Y") epg3_day = epg_time_2.strftime("%d") epg3_month = epg_time_2.strftime("%m") epg3_year = epg_time_2.strftime("%Y") half_hour = str(epg1_day + "/" + epg1_month + "/" + epg1_year + " " + epg_time1) one_hour = str(epg2_day + "/" + epg2_month + "/" + epg2_year + " " + epg_time2) one_hour_half = str(epg3_day + "/" + epg3_month + "/" + epg3_year + " " + epg_time3) #Store the times and date in the list.... self.epg_time_1.append(half_hour) self.epg_time_2.append(one_hour) self.epg_time_3.append(one_hour_half) epg_time_1显示字符串epg_time_211:00PM,我想将时间和日期设置为11:30PM 29/08/2017 11:00PM和{对于epg_time_1 {1}}。如果29/08/2017 11:30PM显示字符串epg_time_2,那么我想将其添加到第二天的日期epg_time_3

如果12:00AM30/08/2017 12:00AM显示字符串epg_time_1epg_time_2,则在接下来的24小时内,我想将时间和日期设置为11:00PM 11:30PM的{​​{1}}和30/08/2017 11:00PM的{​​{1}}。如果epg_time_1显示字符串30/08/2017 11:30PM,那么我想将时间epg_time_2

设置为第二天的日期

如果epg_time_312:00AM显示字符串1/09/2017 12:00AMepg_time_1,我想更改为epg_time_2的上一个日期11:30PM 12:00AM 1}}和epg_time_1。这将取决于我在列表中存储字符串的时间和日期。

您能否告诉我一个示例,我可以使用如何将日期更改为上一个日期并使用python添加到第二天的日期?

1 个答案:

答案 0 :(得分:0)

你的问题中有很多文字使得很难准确地找出问题所在。但是,它似乎归结为在特定日期添加可变天数并确保月份也更新(如有必要)。

您应该使用timedelta方法将日期转换为日期时间,这使得添加timedelta变得微不足道(您同时使用strftimeimport datetime as dt def add_days(datelist, days_to_add): # Convert the strings to datetime objects datelist = [dt.datetime.strptime(item, '%d/%m/%Y %I:%M%p') for item in datelist] # Add a variable number of days (can be negative) to the datetimes mod_dates = [item + dt.timedelta(days=days_to_add) for item in datelist] # Convert back to strings str_dates = [dt.datetime.strftime(item, '%d/%m/%Y %I:%M%p') for item in mod_dates] return str_dates # This is the list right at the top of your question a = ['29/08/2017 11:30PM', '30/08/2017 12:00AM', '30/08/2017 12:30AM'] print("Start:") print(a) b = add_days(a, 1) print("After 1 call:") print(b) c = add_days(b, 1) print("After 2 calls:") print(c) 但是错过了这个关键方法)然后只需转换回字符串。

$s = 'abcdefg';