3

I am using a time.struct_time to set up the date and time and I want to add to the next day using a python script.

I am using following code:

date_1 = "31/08/2017 10:30PM"
time_1 = time.strptime(date_1, '%d/%m/%Y %I:%M%p')

Output:

time.struct_time(tm_year=2017, tm_mon=8, tm_mday=31, tm_hour=22, tm_min=30, tm_sec=0, tm_wday=3, tm_yday=243, tm_isdst=-1)

Now I want to add 1 day to this date. I used the following code:

time.struct_time(tm_year=2017, tm_mon=9, tm_mday=1, tm_hour=22, tm_min=30, tm_sec=0, tm_wday=3, tm_yday=243, tm_isdst=-1)

I am new on this, so can you please show me an example how I could add to the next day date using with my code?

Daniel Ezekiel
  • 133
  • 2
  • 4
  • 11

2 Answers2

2

Honestly, I'm not sure if there even is a (clean) way. The data of a struct_time object are stored as tuples, so they are immutable. This means they can't really be modified in place.

I think you should be using the datetime module instead, like so:

>>> from datetime import datetime
>>> date_1 = "31/08/2017 10:30PM"
>>> time_1 = datetime.strptime(date_1, "%d/%m/%Y %I:%M%p")
>>> time_1
datetime.datetime(2017, 8, 31, 22, 30)

You can then add a month like so:

>>> from dateutil.relativedelta import relativedelta
>>> time_1 + relativedelta(months=1)
datetime.datetime(2017, 9, 30, 22, 30)

If you want to add a single day, then use datetime.timedelta(), which deals with the month's number of days:

from datetime import timedelta
time_1 + timedelta(days=1)

The reason I didn't use this above is because timedelta() doesn't take months as an argument


If you wish to convert it back to a string, then you use the strftime() function like so:

>>> new_time_1 = time_1 + relativedelta(months=1)
>>> print(datetime.strftime(new_time_1, '%d/%m/%Y'))
30/09/2017
>>> print(datetime.strftime(time_1, '%d/%m/%Y'))
31/08/2017

If for some reason you need to use the time module (say for one part of your code) but you want to convert it to the datetime object (for reasons like this), then I refer you to this question.

TerryA
  • 58,805
  • 11
  • 114
  • 143
  • thank you very much for this, I'm stored the date_1 string in the list so I could output them when I use `date_1 = ''.join(str(x) for x in self.date_1)`. How I can get the date `31/08/2017` from the string and how I add it to the next day to make it show `1/09/2017` if there is possible to do in a different way? – Daniel Ezekiel Aug 31 '17 at 22:21
  • @DanielEzekiel Check my edit... does that answer your question? – TerryA Aug 31 '17 at 22:35
  • No not really, when you add the day it should add to the next month if my day is 29, 30, 31 or whatever its. Are there a way to do that without add the month, just the day? – Daniel Ezekiel Aug 31 '17 at 22:42
  • @DanielEzekiel Ah, that's where you'd do `from datetime import timedelta; time_1 + timedelta(days=1)`. I didn't use this in my original answer since `timedelta` doesn't take a `months` argument – TerryA Aug 31 '17 at 22:45
  • Oh I see, so how would I know I need to add to the next month as some months has `30` and some months has `31`? – Daniel Ezekiel Aug 31 '17 at 22:48
  • @DanielEzekiel That honestly depends on what you're trying to do. Do you always want this certain date to be the last day of the month? – TerryA Aug 31 '17 at 22:57
  • I am trying to add to the next day and if the day is 30 or 31 then add 1 for the next month. Is that possible to do that in one line? so what about this `dt = datetime.datetime.fromtimestamp(time.mktime(epg_time_1))` as I get the output `2017-08-31 22:30:00`, is that possible to add the next day to make it to show `2017-09-01 22:30:00`? – Daniel Ezekiel Aug 31 '17 at 23:39
  • @DanielEzekiel Then all you need to do is `dt + datetime.timedelta(days=1)`. The function deals with months having 30/31 days (and 28 for feb) – TerryA Sep 01 '17 at 00:29
1

Consider datetime.timedelta as explained in this question: Adding 5 days to a date in Python

jq170727
  • 13,159
  • 3
  • 46
  • 56