我正在處理我的python腳本以從按鈕對象中獲取字符串,以便我可以使用它來設置日期格式與從字符串獲取的時間以將其存儲在名單。當我從按鈕對象中獲取字符串時,我想爲每個字符串設置日期,例如:29/08/2017 11:30PM
,30/08/2017 12:00AM
,30/08/2017 12:30AM
。在Python中更改爲前一天日期
當我試試這個:
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
,從變量epg_time_1
,epg_time_2
和epg_time_3
11:30PM
和12:00AM
,它會顯示這樣的輸出:
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.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(self)函數時,將每個字符串更改爲前一天的日期。如果epg_time_1
和epg_time_2
顯示字符串11:00PM
和11:30PM
,我要設置爲29/08/2017 11:00PM
的時間和日期epg_time_1
和29/08/2017 11:30PM
爲epg_time_2
。如果epg_time_3
顯示字符串12:00AM
那麼我想將它添加到第二天的日期與時間30/08/2017 12:00AM
。
在未來24小時內,如果epg_time_1
和epg_time_2
顯示字符串11:00PM
和11:30PM
,我要設置爲30/08/2017 11:00PM
的時間和日期epg_time_1
和30/08/2017 11:30PM
的epg_time_2
。如果epg_time_3
顯示字符串12:00AM
那麼我想設置爲第二天的日期與時間1/09/2017 12:00AM
如果epg_time_1
和epg_time_2
顯示字符串11:30PM
和12:00AM
,我想改變以前的日期epg_time_1
它是29/08/2017 11:30PM
和30/08/2017 12:00AM
。這將取決於我將這些字符串存儲在列表中的時間和日期。
你能告訴我一個例子,我可以如何使用將日期更改爲以前的日期並添加到使用Python的第二天日期?
請問您能整理一下這段文字嗎?看起來你在這裏真正需要的是使用'strptime'將你的字符串轉換成日期。日期變化的所有邏輯都將由日期時間模塊本身處理。 – roganjosh
對,我可以把這裏的東西放在一起作爲例子,但我認爲不可能從所呈現的內容中正確回答你的問題。我將不得不做出一些假設;你會得到一個表示不同日期時間的3個字符串的列表,你需要給這些日期添加一個常數因子(1到6之間),並且你希望日期時間反映這個變化? – roganjosh