本文主要是介绍python获取上一季度最后一天,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
闲话少叙,直接上代码
from datetime import datetime
def getlastseason(today):today = datetime.strptime(today, '%Y%m%d')quarter = (today.month - 1) / 3 + 1if quarter == 1:return datetime(today.year - 1, 12, 31).strftime('%Y%m%d')elif quarter == 2:return datetime(today.year, 3, 31).strftime('%Y%m%d')elif quarter == 3:return datetime(today.year, 6, 30).strftime('%Y%m%d')else:return datetime(today.year, 9, 30).strftime('%Y%m%d')
这个函数的参数是字符串日期,比如'20180101',返回的也是字符串的日期,函数调用如下:
这篇关于python获取上一季度最后一天的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!