1.获取本周周一
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import time
from datetime import datetime, timedelta
today = time.strftime("%Y%m%d", time.localtime())
today = datetime.strptime(str(today), "%Y%m%d")
print(datetime.strftime(today - timedelta(today.weekday()), "%Y-%m-%d"))2.获取本周周一日期
用timedelta函数做一个小算法:
from datetime import datetime, timedelta
def this_monday(today):
"""
:function: 获取本周周一日期
:param today:
:return: 返回周一的日期
:return_type: string
"""
today = datetime.strptime(str(today), "%Y%m%d")
return datetime.strftime(today - timedelta(today.weekday()), "%Y%m%d")3.获取本周周日日期
算法同上
from datetime import datetime, timedelta
def this_sunday(today):
"""
:function: 获取本周周日日期
:param today:
:return: 返回周日日期
:return_type: string
"""
today = datetime.strptime(str(today), "%Y%m%d")
return datetime.strftime(today + timedelta(7 - today.weekday() - 1), "%Y%m%d")4.获取当前周 周一和周末时间
import datetime
def get_current_week():
monday, sunday = datetime.date.today(), datetime.date.today()
one_day = datetime.timedelta(days=1)
while monday.weekday() != 0:
monday -= one_day
while sunday.weekday() != 6:
sunday += one_day
return monday, sunday
# 返回时间字符串
# return datetime.datetime.strftime(monday, "%Y/%m/%d") + ' 00:00:00+08:00', datetime.datetime.strftime(sunday, "%Y/%m/%d")+ ' 23:59:59+08:00'
print(get_current_week())参考自:
https://blog.csdn.net/f919976711/article/details/111046649
https://blog.csdn.net/weixin_44541001/article/details/106208969
https://www.runoob.com/python/python-date-time.html