0

How can I get the next date instead of today's date, should handle the month with 28 days like February. I don't want to get 32 December or 30 February :).

PS: sorry for my bad English, not my first language. Also, It's my first question.

My code here:

    def yeartoday(self):
        return datetime.datetime.utcnow().strftime('%Y')

    def monthtoday(self):
        return datetime.datetime.utcnow().strftime('%m')


    def daytoday(self):
        return datetime.datetime.utcnow().strftime('%d')


    def informatdate1(self):
        return "%s-%s-%s" % (self.yeartoday(), self.monthtoday(), self.daytoday())


    def informatdate2(self):
        return "%s|%s|%s" % (self.yeartoday(), self.monthtoday(), self.daytoday())


2 Answers2

0

You need timedelta:

from datetime import timedelta,datetime
datetime.now() + timedelta(days=1)
Pratik
  • 1,351
  • 1
  • 20
  • 37
  • how to get that in format like 2019|11|15 and 2019-11-15? –  Nov 15 '19 at 10:21
  • @raulnasper look at this answer https://stackoverflow.com/questions/10624937/convert-datetime-object-to-a-string-of-date-only-in-python – Pratik Nov 15 '19 at 10:22
0

use timedelta from here https://docs.python.org/3.7/library/datetime.html

you can just do datetime.datetime.now() + datetime.timedelta(1)

EDIT to answer comment

you can still format it like you were doing before with strftime:

my_new_time_var = (datetime.datetime.now()+timedelta(1)).strftime('%y|%m|%d')

vencaslac
  • 2,727
  • 1
  • 18
  • 29