python时间块处理(季度第一天,季度最后一天,本月第一天,本月最后一天,本年第一天,本年最后一天,随机给日期获取想要结果)


"""
通过datetime和dateutil进行常用日期的获取
如:
今年,去年,明年
当前季度
本月,上月,去年同期,今年一月
今天,昨天,明天,
本周、本月、本季度、本年第一天,
本周、本月、本季度、本年最后一天
"""
 
import datetime
 
# 获取常用特定日期(如去年/季度/当月/本周/首日/)
 
# =======================================
# 关于年(今年,去年,明年)
# =======================================
# 今年
today = datetime.date.today()           # 都是基于today, 此时为'2019-02-22',下同
today.year                              # out: 2019
 
# 去年
today = datetime.date.today()
today.year-1                           # out : 2018
 
# 明年
today = datetime.date.today()
today.year+1                           # out : 2020
 
 
# =======================================
# 关于季度(当前季度)
# =======================================
today = datetime.date.today()
quarter = (today.month-1) // 3 + 1
'{}Q{}'.format(today.year, quarter)          # out: '2019Q1'
 
 
# =======================================
# 关于月(本月,上月,当年1月,去年同月)
# =======================================
# 本月(月份)
today = datetime.date.today()
today.month                                        # out: 2
 
# 本月(年月)
today = datetime.date.today()
datetime.datetime.strftime(today, "%Y-%m")             # out: '2019-02'
 
# 上月
today = datetime.date.today()
last_month = today + datetime.timedelta(days=-today.day)
# last_month.month                                     # 月份,out: 1
datetime.datetime.strftime(last_month,"%Y-%m")             # out: '2019-01'
 
# 上N个月
from dateutil.relativedelta import relativedelta           # 需要引入新的包
today = datetime.date.today()
last_2_month = today + relativedelta(months=-2)           # 上两个月,上N个月参数为(months=-N)
datetime.datetime.strftime(last_2_month, "%Y-%m")             # out: '2018-12'
 
# 当年1月
today = datetime.date.today()
str(today.year) +'-01'              # out: '2019-01'
 
 
# =======================================
# 关于天
# =======================================
# 今天(天数)
today = datetime.date.today()
today.day
# 今天(日期)
today.isoformat()                # out: '2019-02-22'
 
#  昨天
today = datetime.date.today()
yesterday = today + datetime.timedelta(days=-1)
yesterday.isoformat()                                   # out: '2019-02-21'
 
# 明天
today = datetime.date.today()
yesterday = today + datetime.timedelta(days=1)
yesterday.isoformat()                                   # out: '2019-02-23'
 
# 本周第一天
today = datetime.date.today()
week_start_day = today - datetime.timedelta(days=today.weekday())
week_start_day.isoformat()                                   # '2019-02-18',周一
 
# 本周最后一天
today = datetime.date.today()
week_end_day = today + datetime.timedelta(days=6-today.weekday())
week_end_day.isoformat()                                   # '2019-02-24',周天
 
# 本月第一天
today = datetime.date.today()
month_start_day = today - datetime.timedelta(days=today.day-1)
month_start_day.isoformat()                                   # '2019-02-01',当月第一天
 
# 本月最后一天
from dateutil.relativedelta import relativedelta            # 引入新的包
today = datetime.date.today()
# 必须先置为首日,直接用relativedelta(months=1,days=-today.day+1)会有错误结果
month_end_day = (today + datetime.timedelta(days=-today.day+1)) + relativedelta(months=1,days=-1)
month_end_day.isoformat()                                    # '2019-02-28',当月最后一天
 
# 本季度第一天
today = datetime.date.today()
quarter_start_day = datetime.date(today.year,today.month - (today.month - 1) % 3, 1)
quarter_start_day.isoformat()                              # '2019-01-01'  ,本季度第一天
 
# 本季度最后一天
from dateutil.relativedelta import relativedelta           # 引入新的包
today = datetime.date.today()
quarter_end_day = datetime.date(today.year,today.month - (today.month - 1) % 3 +2, 1) + relativedelta(months=1,days=-1)
quarter_end_day.isoformat()                                # '2019-03-31'  ,本季度最后一天
 
# 本年第一天
today = datetime.date.today()
year_start_day = datetime.date(today.year, 1, 1)
year_start_day.isoformat()                                      # '2019-01-01'  ,本年第一天
 
# 本年最后一天
today = datetime.date.today()
year_end_day = datetime.date(today.year+1, 1, 1) - datetime.timedelta(days=1)
year_end_day.isoformat()                                        # '2019-12-31' ,本年最后一天
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值