【发布时间】:2016-09-20 20:43:24
【问题描述】:
摘自dateutil.relativedelta.relativedelta的描述,
年、月、日、时、分、秒、微秒:
Absolute information (argument is singular); adding or subtracting a relativedelta with absolute information does not perform an aritmetic operation, but rather REPLACES the corresponding value in the original datetime with the value(s) in relativedelta.年、月、周、日、小时、分钟、秒、微秒:
Relative information, may be negative (argument is plural); adding or subtracting a relativedelta with relative information performs the corresponding aritmetic operation on the original datetime value with the information in the relativedelta.
我可以从下面的例子中看出加减法的不同。
>>> from datetime import datetime
>>> from dateutil.relativedelta import relativedelta
>>> now = datetime.now()
>>> str(now)
'2016-05-23 22:32:48.427269'
>>> singular = relativedelta(month=3)
>>> plural = relativedelta(months=3)
# subtracting
>>> str(now - singular) # replace the corresponding value in the original datetime with the value(s) in relativedelta
'2016-03-23 22:32:48.427269'
>>> str(now - plural) # perform the corresponding aritmetic operation on the original datetime value with the information in the relativedelta.
'2016-02-23 22:32:48.427269'
# adding
>>> str(now + singular) # replace the corresponding value in the original datetime with the value(s) in relativedelta
'2016-03-23 22:32:48.427269'
>>> str(now + plural) # perform the corresponding aritmetic operation on the original datetime value with the information in the relativedelta.
'2016-08-23 22:32:48.427269'
除此之外,relativedelta 中的单数和复数参数还有什么区别?
【问题讨论】:
-
“其他差异”是什么意思?您似乎非常有效地捕捉到了整体差异。
-
@Paul,我只是想确保我能很好地理解这些差异,这样我以后就不会犯错了。
标签: python python-dateutil relativedelta