1

I'm trying to add a timedelta of +1 days to my datetime object using:

.timedelta(days=1)

With:

datetime.now(pytz.timezone('Europe/London')).strftime("%d%m%Y")`

But for some reason, it's not working as it doesn't allow it to be put anywhere in that line.

I've also tried:

GMTDAY = datetime.now(pytz.timezone('Europe/London'))
GMTDAY = str(GMTDAY + timedelta(days=1))
GMTDAY = datetime(int(GMTDAY)).strftime("%d%m%Y")

But that returns:

invalid literal for int() with base 10: '2016-04-28 02:50:52.436000+01:00'

Any idea what I am doing wrong and how to solve it?

EDIT:

It's not a duplicate because this one is specifically about doing it with pytz, it's easy to do it without pytz.

Ryflex
  • 5,559
  • 25
  • 79
  • 148
  • 1
    Possible duplicate of [How to add delta to python datetime.time?](http://stackoverflow.com/questions/12448592/how-to-add-delta-to-python-datetime-time) – hichris123 Apr 27 '16 at 01:59
  • After GMTDAY = str(GMTDAY + timedelta(days=1)) you converted GMTDAY to a string; it is not really clear what you are trying to do here. – Cyb3rFly3r Apr 27 '16 at 02:07
  • related: [Get yesterday's date in Python, DST-safe](http://stackoverflow.com/a/15345272/4279) – jfs Apr 27 '16 at 17:04
  • related: [How can I subtract a day from a Python date?](http://stackoverflow.com/q/441147/4279) – jfs Apr 27 '16 at 17:05

1 Answers1

2

If I interpreted correctly what you are trying to do, try something like:

GMTDAY = datetime.now(pytz.timezone('Europe/London'))
GMTDAY += timedelta(days=1)
tomorrow = GMTDAY.strftime("%d-%m-%Y")
print(tomorrow)

Output:

28-04-2016
Cyb3rFly3r
  • 1,321
  • 7
  • 12
  • I turned it into a string based on some other stackoverflow posts, is it possible to condesne that into a single line bit of code? – Ryflex Apr 27 '16 at 03:21
  • @Ryflex: if the `+DAY` moves the date across a DST boundary then you have to use `pytz_timezone.normalize()` method to get the correct local time. – jfs Apr 27 '16 at 17:02