Python 日期區(qū)間處理 (本周本月上周上月...)
工具類
class CalendarUtils:
"""
日期工具類
"""
@staticmethod
def delta_day(delta=0):
"""
:param delta: 偏移量
:return: 0今天, 1昨天, 2前天, -1明天 ...
"""
return (datetime.now() + timedelta(days=delta)).strftime('%Y-%m-%d')
@staticmethod
def delta_week(delta=0):
"""
:param delta: 偏移量
:return: 0本周, -1上周, 1下周 ...
"""
now = datetime.now()
week = now.weekday()
_from = (now - timedelta(days=week - 7 * delta)).strftime('%Y-%m-%d')
_to = (now + timedelta(days=6 - week + 7 * delta)).strftime('%Y-%m-%d')
return _from, _to
@staticmethod
def delta_month(delta=0):
"""
:param delta: 偏移量
:return: 0本月, -1上月, 1下月, 下下個月...
"""
def _delta_month(__year, __month, __delta):
_month = __month + __delta
if _month < 1:
delta_year = math.ceil(abs(_month) / 12)
delta_year = delta_year if delta_year else 1
__year -= delta_year
_month = delta_year * 12 + __month + __delta
elif _month > 12:
delta_year = math.floor(_month / 12)
__year += delta_year
_month %= 12
return __year, _month
now = datetime.now()
_from = datetime(*_delta_month(now.year, now.month, delta), 1)
_to = datetime(*_delta_month(_from.year, _from.month, 1), 1) - timedelta(days=1)
return _from.strftime('%Y-%m-%d'), _to.strftime('%Y-%m-%d')
@staticmethod
def delta_year(delta=0):
"""
:param delta: 偏移量
:return: 0今年, -1去年, 1明年 ...
"""
now = datetime.now()
_from = datetime(now.year + delta, 1, 1)
_to = datetime(_from.year + 1, 1, 1) - timedelta(days=1)
return _from.strftime('%Y-%m-%d'), _to.strftime('%Y-%m-%d')
if __name__ == '__main__':
print('當(dāng)前日期: ', datetime.now())
print('*' * 40)
print('今天: ', CalendarUtils.delta_day())
print('昨天: ', CalendarUtils.delta_day(-1))
print('前天: ', CalendarUtils.delta_day(-2))
print('明天: ', CalendarUtils.delta_day(1))
print('后天: ', CalendarUtils.delta_day(2))
print('*' * 40)
print('本周: ', CalendarUtils.delta_week())
print('上周: ', CalendarUtils.delta_week(-1))
print('下周: ', CalendarUtils.delta_week(1))
print('*' * 40)
print('本月: ', CalendarUtils.delta_month())
print('上月: ', CalendarUtils.delta_month(-1))
print('下月: ', CalendarUtils.delta_month(1))
print('*' * 40)
print('本年: ', CalendarUtils.delta_year())
print('去年: ', CalendarUtils.delta_year(-1))
print('明年: ', CalendarUtils.delta_year(1))
運行結(jié)果
當(dāng)前日期: 2019-06-26 11:01:34.662560
****************************************
今天: 2019-06-26
昨天: 2019-06-25
前天: 2019-06-24
明天: 2019-06-27
后天: 2019-06-28
****************************************
本周: ('2019-06-24', '2019-06-30')
上周: ('2019-06-17', '2019-06-23')
下周: ('2019-07-01', '2019-07-07')
****************************************
本月: ('2019-06-01', '2019-06-30')
上月: ('2019-05-01', '2019-05-31')
下月: ('2019-07-01', '2019-07-31')
****************************************
本年: ('2019-01-01', '2019-12-31')
去年: ('2018-01-01', '2018-12-31')
明年: ('2020-01-01', '2020-12-31')
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
windows系統(tǒng)下Python環(huán)境的搭建(Aptana Studio)
這篇文章主要介紹了windows系統(tǒng)下Python環(huán)境的搭建(Aptana Studio),需要的朋友可以參考下2017-03-03
python通過函數(shù)屬性實現(xiàn)全局變量的方法
這篇文章主要介紹了python通過函數(shù)屬性實現(xiàn)全局變量的方法,實例分析了Python中函數(shù)屬性的相關(guān)使用技巧,需要的朋友可以參考下2015-05-05
對python特殊函數(shù) __call__()的使用詳解
今天小編就為大家分享一篇對python特殊函數(shù) __call__()的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
使用Python實現(xiàn)文本情感分析預(yù)處理的詳細教程
在自然語言處理(NLP)領(lǐng)域,文本情感分析是一項重要任務(wù),它旨在通過計算機技術(shù)識別和提取文本中的情感傾向(如正面、負面或中性),為了實現(xiàn)準確的情感分析,預(yù)處理步驟至關(guān)重要,所以本文給大家介紹了使用Python實現(xiàn)文本情感分析預(yù)處理的詳細教程,需要的朋友可以參考下2025-04-04
PyQt5連接MySQL及QMYSQL driver not loaded錯誤解決
這篇文章主要介紹了PyQt5連接MySQL及QMYSQL driver not loaded錯誤解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python利用redis-py實現(xiàn)集合與有序集合的常用指令操作
這篇文章我們將來學(xué)習(xí)?redis-py?這個模塊針對?“集合”?與?"有序集合"的一些常用指令操作,文中的示例代碼講解詳細,需要的可以參考一下2022-09-09
???????Python?入門學(xué)習(xí)之函數(shù)式編程
這篇文章主要介紹了???????Python?入門學(xué)習(xí)之函數(shù)式編程,?Python?中的函數(shù)式編程技術(shù)進行了簡單的入門介紹,下文詳細內(nèi)容需要的小伙伴可以參考一下2022-05-05

