0

So basically I want to check if a certain string includes tommorows date so i made this date variable (see code). Now the problem is every month on the last day this is going to be wrong. For example the 30th of september, with the way i did it, its going to say that tommorow will be the 31 of september, wich does not exist however. I need it to say that tommorow is the 1. of october. Any suggestions? It has to be in the format dd.mm.yy please.

  day = str(datetime.datetime.today().day+1)
  month = str(datetime.datetime.today().month)
  year = str(datetime.datetime.today().year)      
  date = day + "." + month + "." + year
WiHa
  • 35
  • 2
  • 4
  • 3
    `str(datetime.datetime.today() + datetime.timedelta(days=1))` – Joran Beasley Nov 05 '18 at 17:46
  • @JoranBeasley If you flesh that out a bit, that could definitely be posted as an answer. – mypetlion Nov 05 '18 at 17:46
  • 1
    A word of advice: Only call `datetime.datetime.today()` _once_, and base all calculations (day, month, year, etc.) on that. Otherwise there's a possibility of a 31st of November etc. (if the first line executes on 31st of October at 23:59:59.987, and the second line on 1st of November, 00:00:00.014). – L3viathan Nov 05 '18 at 17:49

2 Answers2

3

just add one day to today

tomorrow = datetime.datetime.today() + datetime.timedelta(days=1)
print(str(tomorrow),tomorrow.strftime("%d.%m.%y"))
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

relativedelta can also be used

import datetime
from dateutil.relativedelta import relativedelta 
tomorrow = datetime.datetime.today() + relativedelta(days=1)
print(str(tomorrow),tomorrow.strftime("%d.%m.%y"))
mad_
  • 8,121
  • 2
  • 25
  • 40
  • When adding a delta of 1 day, what advantage does using a `relativedelta` (which requires adding a third-party dependency) have over a `timedelta` (which is standard library)? – wim Nov 05 '18 at 18:34