Jump to content
Sign in to follow this  
ameliabob

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...