Learn Before
Code

Examples

from datetime import *; from dateutil.relativedelta import * import calendar NOW = datetime.now() TODAY = date.today()

NOW would include the datetime values year to seconds and TODAY would include the values year, month, and day.

The following snippet will move the date to the next month.

>>> NOW+relativedelta(months=+1) datetime.datetime(2003, 10, 17, 20, 54, 47, 282310)

The following snippet will move the time to next month, one week ahead, at 10 am.

>>> TODAY+relativedelta(months=+1, weeks=+1, hour=10) datetime.datetime(2003, 10, 24, 10, 0)

relativedate also allows the user to calculate the difference between dates and time.

>>> relativedelta(datetime(2003, 10, 24, 10, 0), TODAY) relativedelta(months=+1, days=+7, hours=+10)

0

1

Updated 2021-07-13

Tags

Python Programming Language

Data Science

Related