我需要你帮助我当前的代码。我想使用time.struct_time
对象添加第二天的日期,但在将该字符串存储在self.epg_time_1
列表中后,我不知道如何执行此操作。
实施例: time.struct_time(tm_year = 2018,tm_mon = 2,tm_mday = 22,tm_hour = 17,tm_min = 00,tm_sec = 0,tm_wday = 3,tm_yday = 53,tm_isdst = -1)
对此:
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=23, tm_hour=17, tm_min=00, tm_sec=0, tm_wday=3, tm_yday=53, tm_isdst=-1)[/python]
如果我想在第二天添加,我必须阅读我已经使用的相同代码:
epg_time_1_days = time.strftime("%d")
epg_time_1_months = time.strftime("%m")
epg_time_1_year = time.strftime("%Y")
epg_time_1_days = int(time.strftime("%d") + 1)
epg_time_1_days = str(today_day)
epg_time_1_months = str(epg_time_1_months)
epg_time_1_year = str(epg_time_1_year)
half_hour_date = str(epg_time_1_days + "/" + epg_time_1_months + "/" + epg_time_1_year + " " + "17:30PM")
self.epg_time_1.append(half_hour_date)
以下是代码:
half_hour_date = ''.join(str(x) for x in self.epg_time_1)
epg_time_1 = time.strptime(half_hour_date, '%d/%m/%Y %I:%M%p')
half_hour_date
的输出:
22/02/2018 5:00PM
epg_time_1
的输出:
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=22, tm_hour=17, tm_min=00, tm_sec=0, tm_wday=3, tm_yday=53, tm_isdst=-1)
我不想重新添加我已编写过的相同代码。如果您可以向我展示一个示例,我可以使用简短的简单代码将日期添加到使用time.struct_time
对象的第二天日期,那就太棒了。
答案 0 :(得分:0)
您可以改用datetime.datetime
吗?
>>> from datetime import datetime, timedelta
>>> d = datetime.today()
>>> d
datetime.datetime(2018, 2, 22, 16, 25, 1, 596038)
>>> tomorrow = (d + timedelta(days=1))
>>> tomorrow
datetime.datetime(2018, 2, 23, 16, 25, 1, 596038)
>>> d.day, tomorrow.day
(22, 23)