2017-08-31 37 views
1

我正在處理我的python腳本以從按鈕對象中獲取字符串,以便我可以使用它來設置日期格式與從字符串獲取的時間以將其存儲在名單。當我從按鈕對象中獲取字符串時,我想爲每個字符串設置日期,例如:29/08/2017 11:30PM30/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_1epg_time_2epg_time_311:30PM12: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_1epg_time_2顯示字符串11:00PM11:30PM,我要設置爲29/08/2017 11:00PM的時間和日期epg_time_129/08/2017 11:30PMepg_time_2。如果epg_time_3顯示字符串12:00AM那麼我想將它添加到第二天的日期與時間30/08/2017 12:00AM

在未來24小時內,如果epg_time_1epg_time_2顯示字符串11:00PM11:30PM,我要設置爲30/08/2017 11:00PM的時間和日期epg_time_130/08/2017 11:30PMepg_time_2。如果epg_time_3顯示字符串12:00AM那麼我想設置爲第二天的日期與時間1/09/2017 12:00AM

如果epg_time_1epg_time_2顯示字符串11:30PM12:00AM,我想改變以前的日期epg_time_1它是29/08/2017 11:30PM30/08/2017 12:00AM。這將取決於我將這些字符串存儲在列表中的時間和日期。

你能告訴我一個例子,我可以如何使用將日期更改爲以前的日期並添加到使用Python的第二天日期?

+0

請問您能整理一下這段文字嗎?看起來你在這裏真正需要的是使用'strptime'將你的字符串轉換成日期。日期變化的所有邏輯都將由日期時間模塊本身處理。 – roganjosh

+0

對,我可以把這裏的東西放在一起作爲例子,但我認爲不可能從所呈現的內容中正確回答你的問題。我將不得不做出一些假設;你會得到一個表示不同日期時間的3個字符串的列表,你需要給這些日期添加一個常數因子(1到6之間),並且你希望日期時間反映這個變化? – roganjosh

回答

0

在您的問題中有很多文字使得很難精確地查明問題。然而,這似乎歸結爲在特定日期添加可變天數並確保月份也被更新(如有必要)。

您應該使用datetime.datetime.strptime()方法您的日期轉換爲日期時間,這使得它容易加入timedelta(同時使用timedeltastrftime但錯過這個重要的方法),然後就重新轉換爲字符串。

import 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)