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

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

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

前言

本文主要介紹的是關(guān)于利用python 獲取本周,上周,本月,上月,本季的時(shí)間,話不多說了,來一起看看實(shí)現(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獲取本周、上周、本月、上月及本季的時(shí)間的文章就介紹到這了,更多相關(guān)python獲取本周、上周、本月、上月及本季時(shí)間內(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é),本文分別給出實(shí)例講解它們的使用方法,需要的朋友可以參考下
    2015-04-04
  • Python使用pyserial進(jìn)行串口通信的實(shí)例

    Python使用pyserial進(jìn)行串口通信的實(shí)例

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

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

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

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

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

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

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

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

    這篇文章主要給大家介紹了關(guān)于Python除法保留兩位小數(shù)點(diǎn)的三種方法實(shí)現(xiàn),在py應(yīng)用中有許多拿結(jié)果中的多個(gè)整數(shù)進(jìn)行運(yùn)算,難免少不了除法(如單位換算等),但是整數(shù)進(jìn)行運(yùn)算后只會(huì)返回整數(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方法允許在行或列上計(jì)算排名
    2024-09-09
  • python爬蟲之爬取谷歌趨勢數(shù)據(jù)

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

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

    python PyGame五子棋小游戲

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

    Python繪制交通流折線圖詳情

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

最新評論

无棣县| 武义县| 开封县| 安岳县| 荃湾区| 营口市| 来安县| 安陆市| 攀枝花市| 平陆县| 进贤县| 江达县| 中宁县| 南涧| 柳林县| 苏尼特右旗| 景洪市| 新源县| 鲁甸县| 江安县| 黄平县| 五指山市| 麟游县| 达日县| 九龙坡区| 仁寿县| 江安县| 称多县| 利辛县| 栾城县| 华容县| 常州市| 马公市| 汝阳县| 南汇区| 南华县| 尖扎县| 湘西| 航空| 诸城市| 兴安盟|