0

I have the following code:

def dates_sec():
    START_DATE = str(input('Enter start date in %Y-%m-%d %H:%M:%S format: '))
    END_DATE = str(input('Enter end date in %Y-%m-%d %H:%M:%S format: '))

    try:
        START_DATE = datetime.datetime.strptime(START_DATE, '%Y-%m-%d %H:%M:%S')
        END_DATE = datetime.datetime.strptime(END_DATE, '%Y-%m-%d %H:%M:%S')
        start_date = START_DATE.strftime('%Y%m%d')
        end_date = END_DATE.strftime('%Y%m%d')
        print('Dates accepted!')
        return start_date, end_date
    except ValueError:
        raise ValueError("Incorrect date format, should be YYYY-MM-DD HH:MM:SS")

SD, ED = dates_sec()

Here, I take 2 dates as input from the user. Now, Based on what the END_DATE is inputted by the user, I would like to generate a new variable named NEXT_DATE which contains the next day's date in %Y%m%d form.

Example:

If I input START_DATE as 2018-07-01 00:00:00

and

the END_DATE as 2018-07-01 00:00:00,

then the varibles SD & EDare 20180701 & 20180731 respectively.

Now, based on the END_DATE, the NEXT_DATE must be 2018-08-01 00:00:00 and must be returned as 20180801.

I would like to know how can I do this.

some_programmer
  • 3,268
  • 4
  • 24
  • 59
  • Possible duplicate of [How to increment a datetime by one day?](https://stackoverflow.com/questions/3240458/how-to-increment-a-datetime-by-one-day) – Silveris Aug 30 '19 at 09:44

1 Answers1

1

You are looking for:

NEXT_DATE = END_DATE + datetime.timedelta(days=1)
next_date = NEXT_DATE.strftime('%Y%m%d')
tituszban
  • 4,797
  • 2
  • 19
  • 30