python獲取本周、上周、本月、上月及本季的時(shí)間代碼實(shí)例
前言
本文主要介紹的是關(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)文章希望大家以后多多支持腳本之家!
- python 代碼運(yùn)行時(shí)間獲取方式詳解
- Python sqlalchemy時(shí)間戳及密碼管理實(shí)現(xiàn)代碼詳解
- Python代碼執(zhí)行時(shí)間測量模塊timeit用法解析
- Python實(shí)現(xiàn)進(jìn)度條和時(shí)間預(yù)估的示例代碼
- 利用4行Python代碼監(jiān)測每一行程序的運(yùn)行時(shí)間和空間消耗
- python 實(shí)現(xiàn)仿微信聊天時(shí)間格式化顯示的代碼
- Python文件時(shí)間操作步驟代碼詳解
- Python計(jì)算公交發(fā)車時(shí)間的完整代碼
- Python實(shí)現(xiàn)bilibili時(shí)間長度查詢的示例代碼
- Python統(tǒng)計(jì)時(shí)間內(nèi)的并發(fā)數(shù)代碼實(shí)例
- 如何基于python測量代碼運(yùn)行時(shí)間
- python 統(tǒng)計(jì)代碼耗時(shí)的幾種方法分享
相關(guān)文章
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í)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
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)制問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
在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)
這篇文章主要給大家介紹了關(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中的rank方法是用于數(shù)據(jù)排名的重要工具,它不返回排序后的數(shù)據(jù),而是數(shù)據(jù)的排名。rank方法可以處理相同數(shù)據(jù)的排名,通過平均排名方式解決排名沖突,并支持自定義排序規(guī)則及逆序排名。此外,DataFrame的rank方法允許在行或列上計(jì)算排名2024-09-09

