Jump to content
Sign in to follow this  

calendar arithmetic

Recommended Posts

I can see functions allowing the calculations of the difference between two dates but I cannot locate how to find the addition or subtraction of x number of days from a given date.

Is there such a thing in python?

Share this post

Link to post
Share on other sites

You create a date() object, and then you can use a timedelta() object to operate with it. Examples are shown here: https://docs.python.org/3/library/datetime.html#datetime.date

A short program to add a number of days to a date might look like this:

d = date(2018, 7, 20)
delta = timedelta(days=5)
d2 = d + delta

There might be a shorter way to write it, but I don't work with Python very often so I'd have to look it up to make sure.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this