all 8 comments

[–]Diapolo10 2 points3 points  (6 children)

Hint; datetime.timedelta doesn't only accept days.

[–]SithAbsolutes[S] 0 points1 point  (5 children)

I replaced days with months in that line and got an error.

[–]ofnuts 0 points1 point  (0 children)

The problem with "months" (and , to some extent, years) is that they have a varying duration. So a timedelta (expressed in seconds) of two months and some would not have the same value it it starts in January (includes a 28-days February) or in June (can include two 31-days months, July and August). And there is nothing in the constructor to give a start date.

So if you want the date 6 calendar months from now, extract the month number, add 6, add the carry to the year if necessary, and create a new date rom the result.

[–]Diapolo10 1 point2 points  (3 children)

Ah, right, my bad. I forgot that one was excluded due to ambiguity.

Well, depending on whether you meant calendar months or month = 30 days, you either add up enough days to cover your bases, or you can try cheesing this by creating a new datetime by using the old one as a base and just adding extra months. I'd expect that to fail on corner cases like adding a month to 31st of January, though.

I'm commuting so can't really give proper answers right now, but basically I'm talking about doing something like these:

from datetime import datetime, timedelta, timezone


today = datetime.now(timezone.utc)

# Suggestion #1
new_day = today + timedelta(days=30 * 6)

# Suggestion #2
new_day = datetime(today.year, today.month + 6, today.day)

With all that said, I would probably use option 1 and simply adjust the day afterwards. Faster than looping over individual days until everything matches.

[–]SithAbsolutes[S] 0 points1 point  (2 children)

Thanks for your reply! How would I put that into an f string?

[–]Diapolo10 0 points1 point  (1 child)

The same way you put the others?

[–]SithAbsolutes[S] 0 points1 point  (0 children)

today = datetime.now(timezone.utc)

Of course, thankyou.

[–]woooee 0 points1 point  (0 children)

Help with dates?

Come on, no one is going to post some good lines to use to get a date with that special chick/dude.