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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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?
@DanielEzekiel Check my edit... does that answer your question?
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?
@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
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?
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.