Using datetime.timedelta to add years

杀马特。学长 韩版系。学妹 提交于 2020-01-03 19:43:07

问题


I am doing some time calculations in Python.

Goal:

Part of this is trying to :

Given a date, add time interval (X years, X months, X weeks), return date

ie

  • input args: input_time (datetime.date), interval (datetime.timedelta)
  • return: datetime.date

I looked at the datetime and datetime.timedelta docs

class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)¶.

These seem to work well if I want to add a certain number of hours or weeks. However,

Problem:

  • I am trying to implement an operation such as date + 1 year and can't figure it out

E.g.

start = datetime.datetime(2000, 1, 1)
# expected output: datetime.datetime(2001, 1, 1)


# with the weeks, etc arguments given in timedelta, this fails unsurprisingly e.g 
start + datetime.timedelta(weeks = 52)

# returns datetime.datetime(2000, 12, 30, 0, 0)

Question

  • Is this year-based operation possible with the basic tools of datetime - if so, how would I go about it?

  • I realize that for the year example, I could just do start.replace(year = 2001), but that approach will fail if I have months or weeks as input.

  • From my understanding, the dateutil library has more advanced features, but I was not clear how well it interacts with the in-built datetime objects.

I have reviewed this similar question but it did not help me with this.

Any help is much appreciated!

Running Python 3.6.5 on MacOs.


回答1:


timedelta does not support years, because the duration of a year depends on which year (for example, leap years have Feb 29).

You could use a relativedelta instead, which does support years and takes into account the baseline date for additions:

>>> from dateutil.relativedelta import relativedelta
>>> now = datetime.now()
>>> now 
datetime.datetime(2019, 1, 27, 19, 4, 11, 628081)
>>> now + relativedelta(years=1)
datetime.datetime(2020, 1, 27, 19, 4, 11, 628081)



回答2:


You can hard code a new year value of the datetime using replace instead :) This avoids leap years etc.

year_later = current.replace(year=current.year + 1)


来源:https://stackoverflow.com/questions/54394327/using-datetime-timedelta-to-add-years

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!