使用变量

时间:2017-07-30 22:11:41

标签: python python-2.7

我的代码需要一些帮助。我在设置日期格式时遇到问题。我使用了值int(next_day) + 1,因此我可以将这一天添加到第二天的日期。示例:从此01/07/2017到此02/07/2017,依此类推。

以下是我使用的内容:

if day_date == 1:
   if half_hour == "12:00AM":
      print "you are in the epg_time_1 in day 1 at 12am area...................."
      epg_time_1_days = int(next_day) + 1
      epg_time_1_days = str(epg_time_1_days)
      epg_time_1_months = str(today_month)
      epg_time_1_year = str(today_year)
      epg_time_2_days = str(epg_time_1_days)
      epg_time_2_months = str(epg_time_1_months)
      epg_time_2_year = str(epg_time_1_year)
      epg_time_3_days = int(epg_time_2_days)
      epg_time_3_days = str(epg_time_3_days)
      epg_time_3_months = str(epg_time_2_months)
      epg_time_3_year = str(epg_time_2_year)
      elif one_hour == "12:00AM":
      print "you are in the epg_time_2 in day 1 at 12am area...................."
      epg_time_1_days = int(next_day)
      epg_time_1_days = str(epg_time_1_days)
      epg_time_1_months = str(today_month)
      epg_time_1_year = str(today_year)
      epg_time_2_days = int(epg_time_1_days) + 1
      epg_time_2_days = str(epg_time_2_days)
      epg_time_2_months = str(epg_time_1_months)
      epg_time_2_year = str(epg_time_1_year)
      epg_time_3_days = str(epg_time_2_days)
      epg_time_3_months = str(epg_time_2_months)
      epg_time_3_year = str(epg_time_2_year)
   else:
      print "you are in the epg_time_3 in day 1 at 12am area...................."
      epg_time_1_days = int(next_day)
      epg_time_1_days = str(epg_time_1_days)
      epg_time_1_months = str(today_month)
      epg_time_1_year = str(today_year)
      epg_time_2_days = str(epg_time_1_days)
      epg_time_2_months = str(epg_time_1_months)
      epg_time_2_year = str(epg_time_1_year)
      epg_time_3_days = int(epg_time_2_days)
      epg_time_3_days = str(epg_time_3_days)
      epg_time_3_months = str(epg_time_2_months)
      epg_time_3_year = str(epg_time_2_year)
   elif day_date == 2:
      print "you are in the day 2 area...................."
      epg_time_1_days = int(next_day)
      epg_time_1_days = str(epg_time_1_days)
      epg_time_1_months = str(today_month)
      epg_time_1_year = str(today_year)
      epg_time_2_days = int(epg_time_1_days)
      epg_time_2_days = str(epg_time_2_days)
      epg_time_2_months = str(epg_time_1_months)
      epg_time_2_year = str(epg_time_1_year)
      epg_time_3_days = str(epg_time_2_days)
      epg_time_3_months = str(epg_time_2_months)
      epg_time_3_year = str(epg_time_2_year)
   elif day_date == 3:
      print "you are in the day 3 area...................."
      epg_time_1_days = int(next_day)
      epg_time_1_days = str(epg_time_1_days)
      epg_time_1_months = str(today_month)
      epg_time_1_year = str(today_year)
      epg_time_2_days = int(epg_time_1_days)
      epg_time_2_days = str(epg_time_2_days)
      epg_time_2_months = str(epg_time_1_months)
      epg_time_2_year = str(epg_time_1_year)
      epg_time_3_days = str(epg_time_2_days)
      epg_time_3_months = str(epg_time_2_months)
      epg_time_3_year = str(epg_time_2_year)
   elif day_date == 4:
      print "you are in the day 4 area...................."
      epg_time_1_days = int(next_day)
      epg_time_1_days = str(epg_time_1_days)
      epg_time_1_months = str(today_month)
      epg_time_1_year = str(today_year)
      epg_time_2_days = int(epg_time_1_days)
      epg_time_2_days = str(epg_time_2_days)
      epg_time_2_months = str(epg_time_1_months)
      epg_time_2_year = str(epg_time_1_year)
      epg_time_3_days = str(epg_time_2_days)
      epg_time_3_months = str(epg_time_2_months)
      epg_time_3_year = str(epg_time_2_year)
   elif day_date == 5:
      print "you are in the day 5 area...................."
      epg_time_1_days = int(next_day)
      epg_time_1_days = str(epg_time_1_days)
      epg_time_1_months = str(today_month)
      epg_time_1_year = str(today_year)
      epg_time_2_days = int(epg_time_1_days)
      epg_time_2_days = str(epg_time_2_days)
      epg_time_2_months = str(epg_time_1_months)
      epg_time_2_year = str(epg_time_1_year)
      epg_time_3_days = str(epg_time_2_days)
      epg_time_3_months = str(epg_time_2_months)
      epg_time_3_year = str(epg_time_2_year)
   elif day_date == 6:
      print "you are in the day 6 area...................."
      epg_time_1_days = int(next_day)
      epg_time_1_days = str(epg_time_1_days)
      epg_time_1_months = str(today_month)
      epg_time_1_year = str(today_year)
      epg_time_2_days = int(epg_time_1_days)
      epg_time_2_days = str(epg_time_2_days)
      epg_time_2_months = str(epg_time_1_months)
      epg_time_2_year = str(epg_time_1_year)
      epg_time_3_days = str(epg_time_2_days)
      epg_time_3_months = str(epg_time_2_months)
      epg_time_3_year = str(epg_time_2_year)
   else:
      print "you are in the pm else area...................."
      epg_time_1_days = int(today_day) + 1
      epg_time_1_days = str(epg_time_1_days)
      epg_time_1_months = str(today_month)
      epg_time_1_year = str(today_year)
      epg_time_2_days = str(epg_time_1_days)
      epg_time_2_months = str(epg_time_1_months)
      epg_time_2_year = str(epg_time_1_year)
      epg_time_3_days = str(epg_time_2_days)
      epg_time_3_months = str(epg_time_2_months)
      epg_time_3_year = str(epg_time_2_year)
   half_hour_date = str(epg_time_1_days + "/" + epg_time_1_months + "/" + epg_time_1_year + " " + half_hour)
   one_hour_date = str(epg_time_2_days + "/" + epg_time_2_months + "/" + epg_time_2_year + " " + one_hour)
   one_hour_half_date = str(epg_time_3_days + "/" + epg_time_3_months + "/" + epg_time_3_year + " " + one_hour_half)

我的代码存在问题,当我的日期格式显示为30/07/2017时,它会将31/07/201732/07/2017添加到epg_time_1,{{1} }和epg_time_2

我希望通过使用变量epg_time_3,{{我希望设置日期格式30/07/201701/08/201702/08/2017 1}}和epg_time_1,但我不知道该怎么做。

你能否告诉我一个例子,我可以用最好的方法设置这些变量的日期格式?

编辑:这是我想要实现的一个例子:

epg_time_2

1 个答案:

答案 0 :(得分:1)

不要尝试自己编写逻辑周期日期....使用python标准库中的datetime模块。日期复杂而繁琐,有许多角落案例,如闰年来处理。节省一堆工作并使用现有的可信库。

我并非100%确定您的使用案例,但是下面是一个示例函数,它相当于您的" next_day"用例,你可以使它更简洁但我已经将其分解并对其进行了大量评论以帮助理解:

from datetime import datetime, date, timedelta


def get_future_date(date_string, days_in_future, date_format='%d/%m/%Y'):
    intial_date_time  = datetime.strptime(date_string, date_format) # parse the string into a datetime object
    date_only = intial_date_time.date()  #  remove time component so  you have a date object
    future_date = date_only + timedelta(days=days_in_future) # add a time delta to get a future date
    return future_date.strftime(date_format)  # return in the same format as the input

assert  get_future_date("01/07/2017", 1) == "02/07/2017"