亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python獲取本周、上周、本月、上月及本季的時間代碼實例

 更新時間:2020年09月08日 10:43:10   作者:mmmmm__yyy  
這篇文章主要給大家介紹了關(guān)于python獲取本周、上周、本月、上月及本季的時間的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

本文主要介紹的是關(guān)于利用python 獲取本周,上周,本月,上月,本季的時間,話不多說了,來一起看看實現(xiàn)的方法吧

示例代碼:

 
import datetime
from datetime import timedelta
 
now = datetime.datetime.now()
 
# 今天
today = now
print('--- today = {}'.format(today))
 
# 昨天
yesterday = now - timedelta(days=1)
print('--- yesterday = {}'.format(yesterday))
 
# 明天
tomorrow = now + timedelta(days=1)
print('--- tomorrow = {}'.format(tomorrow))
 
# 當(dāng)前季度
now_quarter = now.month / 3 if now.month % 3 == 0 else now.month / 3 + 1
print('--- now_quarter = {}'.format(now_quarter))
 
# 本周第一天和最后一天
this_week_start = now - timedelta(days=now.weekday())
this_week_end = now + timedelta(days=6 - now.weekday())
print('--- this_week_start = {} this_week_end = {}'.format(this_week_start, this_week_end))
 
# 上周第一天和最后一天
last_week_start = now - timedelta(days=now.weekday() + 7)
last_week_end = now - timedelta(days=now.weekday() + 1)
print('--- last_week_start = {} last_week_end = {}'.format(last_week_start, last_week_end))
 
# 本月第一天和最后一天
this_month_start = datetime.datetime(now.year, now.month, 1)
this_month_end = datetime.datetime(now.year, now.month + 1, 1) - timedelta(days=1)+ datetime.timedelta(
	hours=23, minutes=59, seconds=59)
print('--- this_month_start = {} this_month_end = {}'.format(this_month_start, this_month_end))
 
# 上月第一天和最后一天
last_month_end = this_month_start - timedelta(days=1)+ datetime.timedelta(
	hours=23, minutes=59, seconds=59)
last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1)
print('--- last_month_end = {} last_month_start = {}'.format(last_month_end, last_month_start))
 
# 本季第一天和最后一天
month = (now.month - 1) - (now.month - 1) % 3 + 1
this_quarter_start = datetime.datetime(now.year, month, 1)
this_quarter_end = datetime.datetime(now.year, month + 3, 1) - timedelta(days=1)+ datetime.timedelta(
	hours=23, minutes=59, seconds=59)
print('--- this_quarter_start = {} this_quarter_end = {}'.format(this_quarter_start, this_quarter_end))
 
# 上季第一天和最后一天
last_quarter_end = this_quarter_start - timedelta(days=1)+ datetime.timedelta(
	hours=23, minutes=59, seconds=59)
last_quarter_start = datetime.datetime(last_quarter_end.year, last_quarter_end.month - 2, 1)
print('--- last_quarter_start = {} last_quarter_end = {}'.format(last_quarter_start, last_quarter_end))
 
# 本年第一天和最后一天
this_year_start = datetime.datetime(now.year, 1, 1)
this_year_end = datetime.datetime(now.year + 1, 1, 1) - timedelta(days=1)+ datetime.timedelta(
	hours=23, minutes=59, seconds=59)
print('--- this_year_start = {} this_year_end = {}'.format(this_year_start, this_year_end))
 
# 去年第一天和最后一天
last_year_end = this_year_start - timedelta(days=1)+ datetime.timedelta(
	hours=23, minutes=59, seconds=59)
last_year_start = datetime.datetime(last_year_end.year, 1, 1)
print('--- last_year_start = {} last_year_end = {}'.format(last_year_start, last_year_end))

總結(jié)

到此這篇關(guān)于利用python獲取本周、上周、本月、上月及本季的時間的文章就介紹到這了,更多相關(guān)python獲取本周、上周、本月、上月及本季時間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • jupyter?notebook加載和運行.py文件方式

    jupyter?notebook加載和運行.py文件方式

    這篇文章主要介紹了jupyter?notebook加載和運行.py文件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Python requests HTTP驗證登錄實現(xiàn)流程

    Python requests HTTP驗證登錄實現(xiàn)流程

    這篇文章主要介紹了Python requests HTTP驗證登錄實現(xiàn)流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • python爬蟲 正則表達(dá)式使用技巧及爬取個人博客的實例講解

    python爬蟲 正則表達(dá)式使用技巧及爬取個人博客的實例講解

    下面小編就為大家?guī)硪黄猵ython爬蟲 正則表達(dá)式使用技巧及爬取個人博客的實例講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • python cv2截取不規(guī)則區(qū)域圖片實例

    python cv2截取不規(guī)則區(qū)域圖片實例

    今天小編就為大家分享一篇python cv2截取不規(guī)則區(qū)域圖片實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python學(xué)習(xí)之異常中的finally使用詳解

    Python學(xué)習(xí)之異常中的finally使用詳解

    這篇文章主要為大家介紹一下Python異常語法中的另一個成員—finally,通過學(xué)習(xí)finally,可以幫助我們更好的處理異常,快來跟隨小編一起學(xué)習(xí)一下吧
    2022-03-03
  • 淺談Python使用pickle模塊序列化數(shù)據(jù)優(yōu)化代碼的方法

    淺談Python使用pickle模塊序列化數(shù)據(jù)優(yōu)化代碼的方法

    這篇文章主要介紹了淺談Python使用pickle模塊序列化數(shù)據(jù)優(yōu)化代碼的方法,pickle模塊可以對多種Python對象進(jìn)行序列化和反序列化,序列化稱為pickling,反序列化稱為unpickling,需要的朋友可以參考下
    2023-07-07
  • python如何對鏈表操作

    python如何對鏈表操作

    這篇文章主要介紹了python如何對鏈表操作,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-10-10
  • 關(guān)于django 數(shù)據(jù)庫遷移(migrate)應(yīng)該知道的一些事

    關(guān)于django 數(shù)據(jù)庫遷移(migrate)應(yīng)該知道的一些事

    今天小編就為大家分享一篇關(guān)于django 數(shù)據(jù)庫遷移(migrate)應(yīng)該知道的一些事,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 在Linux命令行中運行Python腳本的流程步驟

    在Linux命令行中運行Python腳本的流程步驟

    Python是一種高級編程語言,被廣泛應(yīng)用于數(shù)據(jù)科學(xué)、機器學(xué)習(xí)、Web 開發(fā)等領(lǐng)域,在Linux操作系統(tǒng)中,Python是一個默認(rèn)安裝的解釋器,用戶可以通過命令行界面(CLI)來運行Python腳本,在本文中,我們將詳細(xì)介紹如何在Linux命令行中運行Python腳本,需要的朋友可以參考下
    2023-11-11
  • 圖文詳解Python中最神秘的一個魔法函數(shù)

    圖文詳解Python中最神秘的一個魔法函數(shù)

    Python進(jìn)階之路我覺得有兩個東西一定要了解,一個是魔法函數(shù),下面這篇文章主要給大家介紹了關(guān)于Python中最神秘的一個魔法函數(shù)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-12-12

最新評論