最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年09月08日 10:43:10   作者:mmmmm__yyy  
這篇文章主要給大家介紹了關(guān)于python獲取本周、上周、本月、上月及本季的時間的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(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))
 
# 當前季度
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)文章

  • Python and、or以及and-or語法總結(jié)

    Python and、or以及and-or語法總結(jié)

    這篇文章主要介紹了Python and、or以及and-or語法總結(jié),本文分別給出實例講解它們的使用方法,需要的朋友可以參考下
    2015-04-04
  • Python使用pyserial進行串口通信的實例

    Python使用pyserial進行串口通信的實例

    今天小編就為大家分享一篇Python使用pyserial進行串口通信的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • YOLOv8訓(xùn)練自己的數(shù)據(jù)集(詳細教程)

    YOLOv8訓(xùn)練自己的數(shù)據(jù)集(詳細教程)

    YOLO是一種基于圖像全局信息進行預(yù)測的目標檢測系統(tǒng),YOLOv8 是ultralytics公司在2023年1月10號開源的YOLOv5的下一個重大更新版本,這篇文章主要給大家介紹了關(guān)于YOLOv8訓(xùn)練自己的數(shù)據(jù)集的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • Python輸入正負10進制,轉(zhuǎn)4位16進制問題

    Python輸入正負10進制,轉(zhuǎn)4位16進制問題

    這篇文章主要介紹了Python輸入正負10進制,轉(zhuǎn)4位16進制問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 在python3.64中安裝pyinstaller庫的方法步驟

    在python3.64中安裝pyinstaller庫的方法步驟

    這篇文章主要介紹了在python3.64中安裝pyinstaller庫的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Python除法保留兩位小數(shù)點的三種方法實現(xiàn)

    Python除法保留兩位小數(shù)點的三種方法實現(xiàn)

    這篇文章主要給大家介紹了關(guān)于Python除法保留兩位小數(shù)點的三種方法實現(xiàn),在py應(yīng)用中有許多拿結(jié)果中的多個整數(shù)進行運算,難免少不了除法(如單位換算等),但是整數(shù)進行運算后只會返回整數(shù),一般結(jié)果基本需要精確到后兩位,需要的朋友可以參考下
    2023-08-08
  • pandas中Series和DataFrame的rank方法解析

    pandas中Series和DataFrame的rank方法解析

    pandas中的rank方法是用于數(shù)據(jù)排名的重要工具,它不返回排序后的數(shù)據(jù),而是數(shù)據(jù)的排名。rank方法可以處理相同數(shù)據(jù)的排名,通過平均排名方式解決排名沖突,并支持自定義排序規(guī)則及逆序排名。此外,DataFrame的rank方法允許在行或列上計算排名
    2024-09-09
  • python爬蟲之爬取谷歌趨勢數(shù)據(jù)

    python爬蟲之爬取谷歌趨勢數(shù)據(jù)

    這篇文章主要介紹了python爬蟲之爬取谷歌趨勢數(shù)據(jù),文中有非常詳細的代碼示例,對正在學(xué)習(xí)python爬蟲的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • python PyGame五子棋小游戲

    python PyGame五子棋小游戲

    大家好,本篇文章主要講的是python PyGame五子棋小游戲,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • Python繪制交通流折線圖詳情

    Python繪制交通流折線圖詳情

    這篇文章主要介紹了Python繪制交通流折線圖詳情,文章基于python的相關(guān)資料展開折線圖繪制的實現(xiàn)流程,感興趣的小伙伴可以參考一下
    2022-06-06

最新評論

泾阳县| 阳高县| 平顺县| 永春县| 古丈县| 建水县| 海原县| 年辖:市辖区| 新闻| 黄大仙区| 霍邱县| 缙云县| 多伦县| 孟连| 宣汉县| 英德市| 襄城县| 长治县| 永仁县| 腾冲县| 廉江市| 永定县| 松桃| 郴州市| 达拉特旗| 富平县| 禹城市| 吴堡县| 黑山县| 梅州市| 宝坻区| 卫辉市| 察隅县| 禹城市| 黔江区| 隆尧县| 建阳市| 库尔勒市| 东海县| 商南县| 房山区|