Python calculates the n days after the specified date and the n days before it

  • 2020-09-28 08:59:27
  • OfStack

An example of this article shows how Python calculates the n days and the n days based on a specified date. To share for your reference, the details are as follows:


# -*- coding:utf-8 -*-
#!/usr/bin/python3
import datetime
def getday(y=2017,m=8,d=15,n=0):
 the_date = datetime.datetime(y,m,d)
 result_date = the_date + datetime.timedelta(days=n)
 d = result_date.strftime('%Y-%m-%d')
 return d
print(getday(2017,8,15,21)) #8 month 15 In the future 21 day 
print(getday(2017,9,1,-10)) #9 month 1 recently 10 day 

Operation results:

[

2017-09-05
2017-08-22

]

Here is an additional example of Python using the time module to calculate the date after 100 days:


# -*- coding:utf-8 -*-
#!/usr/bin/python3
import time
time.strftime('%Y%m%d')
import datetime
now_time = datetime.datetime.now()
future_time = now_time + datetime.timedelta(days=100)
fu = future_time.strftime('%Y%m%d')
print(int(fu))

Calculation results:

[

20180906

]

PS: Here are some other online tools for your reference:

Online days calculator:
http://tools.ofstack.com/jisuanqi/datejsq

In the day difference calculator:
http://tools.ofstack.com/jisuanqi/onlinedatejsq

Online date calculator/Difference day calculator:
http://tools.ofstack.com/jisuanqi/datecalc

Online Date/Day calculator:
http://tools.ofstack.com/jisuanqi/date_jisuanqi

More about Python related topics: interested readers to view this site "Python date and time operating skills summary", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"

I hope this article is helpful for Python programming.


Related articles: