SoFunction
Updated on 2024-10-30

Python Date Interval Handling (This Week This Month Last Week Last Month...)

tools

class CalendarUtils:
  """
  Date Tool Class
  """

  @staticmethod
  def delta_day(delta=0):
    """
    :param delta: offset
    :return: 0 today, 1 yesterday, 2 the day before yesterday, -1 tomorrow ...
    """
    return (() + timedelta(days=delta)).strftime('%Y-%m-%d')

  @staticmethod
  def delta_week(delta=0):
    """
    :param delta: offset
    :return: 0 this week, -1 last week, 1 next week ...
    """
    now = ()
    week = ()
    _from = (now - timedelta(days=week - 7 * delta)).strftime('%Y-%m-%d')
    _to = (now + timedelta(days=6 - week + 7 * delta)).strftime('%Y-%m-%d')
    return _from, _to

  @staticmethod
  def delta_month(delta=0):
    """
    :param delta: offset
    :return: 0 this month, -1 last month, 1 next month, next next month...
    """

    def _delta_month(__year, __month, __delta):
      _month = __month + __delta
      if _month < 1:
        delta_year = (abs(_month) / 12)
        delta_year = delta_year if delta_year else 1
        __year -= delta_year
        _month = delta_year * 12 + __month + __delta
      elif _month > 12:
        delta_year = (_month / 12)
        __year += delta_year
        _month %= 12
      return __year, _month

    now = ()
    _from = datetime(*_delta_month(, , delta), 1)

    _to = datetime(*_delta_month(_from.year, _from.month, 1), 1) - timedelta(days=1)
    return _from.strftime('%Y-%m-%d'), _to.strftime('%Y-%m-%d')

  @staticmethod
  def delta_year(delta=0):
    """
    :param delta: offset
    :return: 0 this year, -1 last year, 1 next year ...
    """
    now = ()
    _from = datetime( + delta, 1, 1)
    _to = datetime(_from.year + 1, 1, 1) - timedelta(days=1)
    return _from.strftime('%Y-%m-%d'), _to.strftime('%Y-%m-%d')
    

if __name__ == '__main__':
  print('Current date: ', ())
  print('*' * 40)
  print('Today:', CalendarUtils.delta_day())
  print('Yesterday:', CalendarUtils.delta_day(-1))
  print('The day before yesterday:', CalendarUtils.delta_day(-2))
  print('Tomorrow:', CalendarUtils.delta_day(1))
  print('The day after tomorrow:', CalendarUtils.delta_day(2))
  print('*' * 40)
  print('This week:', CalendarUtils.delta_week())
  print('Last Week:', CalendarUtils.delta_week(-1))
  print('Next week:', CalendarUtils.delta_week(1))
  print('*' * 40)
  print('This month:', CalendarUtils.delta_month())
  print('Last month:', CalendarUtils.delta_month(-1))
  print('Next month:', CalendarUtils.delta_month(1))
  print('*' * 40)
  print('Current year:', CalendarUtils.delta_year())
  print('Last year:', CalendarUtils.delta_year(-1))
  print('Next year:', CalendarUtils.delta_year(1))

running result

Current date: 2019-06-26 11:01:34.662560
****************************************
Today: 2019-06-26
Yesterday: 2019-06-25
Day before yesterday: 2019-06-24
Tomorrow: 2019-06-27
The Day After: 2019-06-28
****************************************
This week: ('2019-06-24', '2019-06-30')
Last week: ('2019-06-17', '2019-06-23')
Next week: ('2019-07-01', '2019-07-07')
****************************************
This month: ('2019-06-01', '2019-06-30')
Previous month: ('2019-05-01', '2019-05-31')
Next month: ('2019-07-01', '2019-07-31')
****************************************
Current Year: ('2019-01-01', '2019-12-31')
Last year: ('2018-01-01', '2018-12-31')
Next year: ('2020-01-01', '2020-12-31')

This is the whole content of this article.