python日期操作:获取本周,上周,本月,上月,本季的时间

DEMO1

获取本周第一天

#/bin/python3 
# addPkg commfunc.py
from pyspark.sql import Row
from pyspark.sql import functions as F
import commfunc

def get_monday():
    datetime = __import__('datetime')
    xingqi = datetime.datetime.now().isoweekday()
    print('今天是星期'+str(xingqi))
    if xingqi == 2:
        return (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d")
    elif xingqi == 1:
        return (datetime.date.today() - datetime.timedelta(days=7)).strftime("%Y%m%d")
    elif xingqi == 3:
        return (datetime.date.today() - datetime.timedelta(days=2)).strftime("%Y%m%d")
    elif xingqi == 4:
        return (datetime.date.today() - datetime.timedelta(days=3)).strftime("%Y%m%d")
    elif xingqi == 5:
        return (datetime.date.today() - datetime.timedelta(days=4)).strftime("%Y%m%d")
    elif xingqi == 6:
        return (datetime.date.today() - datetime.timedelta(days=5)).strftime("%Y%m%d")
    elif xingqi == 7:
        return (datetime.date.today() - datetime.timedelta(days=6)).strftime("%Y%m%d")
    else:
        return ''
        
def main(sparkSession):
    
    print('*************'+str(commfunc.datelable()))
    print('*****&&&&&&*********'+str(datelablee()))

输出:

----------------------------------------------------------------------------------------------------
*************20210412

今天是星期1

*****&&&&&&*********20210412

DEMO2

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))
# 获取一周的日期
now_time = datetime.datetime.now()  # 获取当前时间
day_num = now_time.isoweekday()  # 当前天是这周的第几天
week_start = ((now_time - datetime.timedelta(days=day_num)) + datetime.timedelta(days=1)).date()  # 计算当前天所在周周一 格式为yyyy-MM-dd
week_end = ((now_time - datetime.timedelta(days=day_num)) + datetime.timedelta(days=7)).date()  # 计算当前天所在周周天 格式为yyyy-MM-dd
print(week_end)
week_e = ((now_time - datetime.timedelta(days=day_num)) + datetime.timedelta(days=7))  # 计算当前天所在周周天 格式为yyyy-MM-dd 12:01:13.026315
week_s = ((now_time - datetime.timedelta(days=day_num)) + datetime.timedelta(days=1))  # 计算当前天所在周周一 格式为yyyy-MM-dd 12:01:13.026315
# while week_s <= week_e:  # 循环输出周一至周五的每天日期格式yyyy-MM-dd
    # print(week_s.date())
# week_s = week_s + datetime.timedelta(days=1)


create_time__range = (week_start, week_end)  # 范围(周一,周五)可用于数据库日期范围查询
print(create_time__range)
C:\Users\admin>python date.py
--- today = 2021-04-20 17:12:26.015975
--- yesterday = 2021-04-19 17:12:26.015975
--- tomorrow = 2021-04-21 17:12:26.015975
--- now_quarter = 2.333333333333333
--- this_week_start = 2021-04-19 17:12:26.015975 this_week_end = 2021-04-25 17:12:26.015975
--- last_week_start = 2021-04-12 17:12:26.015975 last_week_end = 2021-04-18 17:12:26.015975
--- this_month_start = 2021-04-01 00:00:00 this_month_end = 2021-04-30 23:59:59
--- last_month_end = 2021-03-31 23:59:59 last_month_start = 2021-03-01 00:00:00
--- this_quarter_start = 2021-04-01 00:00:00 this_quarter_end = 2021-06-30 23:59:59
--- last_quarter_start = 2021-01-01 00:00:00 last_quarter_end = 2021-03-31 23:59:59
--- this_year_start = 2021-01-01 00:00:00 this_year_end = 2021-12-31 23:59:59
--- last_year_start = 2020-01-01 00:00:00 last_year_end = 2020-12-31 23:59:59
2021-04-25
(datetime.date(2021, 4, 19), datetime.date(2021, 4, 25))

参考链接: https://blog.csdn.net/mmmmmm__yy/article/details/108418910


版权声明:本文为qq_39900031原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。