add next day date in time.struct_time object - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: add next day date in time.struct_time object (/thread-8487.html) |
add next day date in time.struct_time object - chris0147 - Feb-22-2018 Hi all, I need your help with my code. I want to add the next day date using with time.struct_time object, but I have got no idea how to do this after I have stored the string in the self.epg_time_1 list.Example: 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)To this: 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)If I want to add the next day, I would have to readd the same code that I have already been using: 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)Here is the code: 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')Output for half_hour_date :22/02/2018 5:00PMOutput for epg_time_1 :[python]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)I don't want to re-add the same code that I have already wrote it. If you can show me an example how I could use a short simple code to add the date to the next day date using with time.struct_time object, that would be great.Thanks in advance RE: add next day date in time.struct_time object - Larz60+ - Feb-22-2018 The following from: https://pymotw.com/3/datetime/ Date math uses the standard arithmetic operators. datetime_date_math.py import datetime today = datetime.date.today() print('Today :', today) one_day = datetime.timedelta(days=1) print('One day :', one_day) yesterday = today - one_day print('Yesterday:', yesterday) tomorrow = today + one_day print('Tomorrow :', tomorrow) print() print('tomorrow - yesterday:', tomorrow - yesterday) print('yesterday - tomorrow:', yesterday - tomorrow)This example with date objects illustrates using timedelta objects to compute new dates, and subtracting date instances to produce timedeltas (including a negative delta value).
RE: add next day date in time.struct_time object - chris0147 - Feb-22-2018 Thank you very much for this, the problem are now being solved. |