python計算,指定的日期,後n天,前n天是哪一天

#!/usr/bin/python
#coding=UTF-8
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月15日後21天
print getday(2017,9,1,-10) #9月1日前10天
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章