H
H
Hassle2021-01-31 21:38:31
Python
Hassle, 2021-01-31 21:38:31

how to do date count in python using datetime()?

Task: Calculate the date in increments, given a range of dates (entered from the keyboard).

Library used: preferably datetime, but if there is a better option, then I don't mind.

What's the problem?: The code I wrote can only count within a range of less than a month.

Initial code:

def datplus(dat, post, ybil):
  yearint = int(dat.year + 1)
  monthint = int(dat.month + 1)
  datint = int(dat.day + 1)
  
  a = post.replace(day = 1)
  b = ybil.replace(day = 1)

  maxdays = b - a
  mindays = int(dat.day) - 1
  mindays += 1

  if mindays == maxdays.days and dat.month == 12:
    try:
      yearplus = dat.replace(day = 1, month = 1, year = yearint)
    except ValueError:
      print("\nОшибка: Программа не может просчитать дату больше 2-х месяцев\nЗавершаю сессию...")
      sys.exit()
    else:
      return yearplus

  elif mindays == maxdays.days:
    try:
      monthplus = dat.replace(day = 1, month = monthint)
    except ValueError:
      print("\nОшибка: Программа не может просчитать дату больше 2-х месяцев\nЗавершаю сессию...")
      sys.exit()
    else:
      return monthplus

  else:
    try:
      datplus = dat.replace(day = datint)
    except ValueError:
      print("\nОшибка: Программа не может просчитать дату больше 2-х месяцев\nЗавершаю сессию...")
      sys.exit()
    else:
      return datplus


The code above is a castile that I did until I solved the problem, but because If you are reading this post, it is clear that I did not succeed.

At the moment, the code has changed a little, and what I did could solve the problem, but I can’t get rid of it, which prevents me from moving to the next month. Part of the modified code:a = post.replace(day = 1)


def datplus(dat, post, ybil):
  yearint = int(dat.year + 1)
  monthint = int(dat.month + 1)
  datint = int(dat.day + 1)
  

  a = post.replace(day = 1)
  aint_month = int(a.month + 1)
  aint_month_year = int(a.year + 1)
  b = a.replace(month = aint_month)

  maxdays = b - a
  mindays = int(dat.day) - 1
  mindays += 1

  if mindays == maxdays.days:
    a.replace(month = aint_month)
    #выполняется только один раз, поэтому на следующий день 'а' снова равен post.replace(day = 1)

  if mindays == maxdays.days and a.month == 12:
    a.replace(month = aint_month, year = yearint)
    b = a.replace(month = aint_month, year = yearint)
    maxdays = b - a
    mindays = int(dat.day) - 1
    mindays += 1
    #выполняется только один раз, поэтому на следующий день 'а' снова равен post.replace(day = 1)

# после if mindays == maxdays.days and dat.month == 12: ничего не менялось

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-01-31
@Hassle

I don't understand what you are trying to do? What inputs throw exceptions?
Use the built-in timedelta function if you need to add something to the next month/year. Well, you can add relativedelta

from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta

now = datetime.now()
# 2021-02-01

print(now + timedelta(days=30))
# 2021-03-03

print(now + timedelta(days=777))
# 2023-03-20

print(now + relativedelta(months=3))
# 2021-05-01

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question