194 lines
6.7 KiB
Python
194 lines
6.7 KiB
Python
#时间日期工具箱
|
||
#author:lybbn
|
||
#QQ:1042594286
|
||
#programe:django-vue-lyadmin
|
||
#date:2023-12-17
|
||
#version:v1.1
|
||
|
||
import re
|
||
import datetime
|
||
|
||
def convertstr2datetime(string,arg="%Y-%m-%d %H:%M:%S"):
|
||
return datetime.datetime.strptime(string, arg)
|
||
|
||
def getTodayRange():
|
||
"""
|
||
获取今天时间范围如:2023-08-02 00:00:01,2023-08-02 23:59:59
|
||
"""
|
||
now = datetime.datetime.now()
|
||
dtstr = now.strftime('%Y-%m-%d')
|
||
starttime = dtstr+" 00:00:01"
|
||
endtime = dtstr + " 23:59:59"
|
||
return starttime,endtime
|
||
|
||
def getLastdayRange():
|
||
"""
|
||
获取昨天时间范围如:2023-08-02 00:00:01,2023-08-02 23:59:59
|
||
"""
|
||
now = datetime.datetime.now()
|
||
last_date = now + datetime.timedelta(days=-1) # 昨天
|
||
dtstr = last_date.strftime('%Y-%m-%d')
|
||
starttime = dtstr+" 00:00:01"
|
||
endtime = dtstr + " 23:59:59"
|
||
return starttime,endtime
|
||
|
||
def getLastdayRangeDate():
|
||
"""
|
||
获取昨天时间范围如:2023-08-02,2023-08-02
|
||
"""
|
||
now = datetime.datetime.now()
|
||
last_date = now + datetime.timedelta(days=-1) # 昨天
|
||
dtstr = last_date.strftime('%Y-%m-%d')
|
||
starttime = dtstr
|
||
endtime = dtstr
|
||
return starttime,endtime
|
||
|
||
def getdayRangeDateByString(string):
|
||
"""
|
||
根据字符串含义获取日期:(最大30天前日期)
|
||
获取昨天时间范围如: string = last1day 取值昨天日期2023-08-02,2023-08-02
|
||
"""
|
||
REGEX_STRING = "^last\d+day$"
|
||
nums = re.findall("\d+", string)
|
||
num = int(nums[0])
|
||
if num>30:
|
||
num = 30
|
||
now = datetime.datetime.now()
|
||
last_date = now + datetime.timedelta(days=-num)
|
||
dtstr = last_date.strftime('%Y-%m-%d')
|
||
starttime = dtstr
|
||
endtime = dtstr
|
||
return starttime,endtime
|
||
|
||
def get7daysRange():
|
||
"""
|
||
获取近7天时间范围如:2023-08-02 00:00:01,2023-08-02 23:59:59
|
||
"""
|
||
now = datetime.datetime.now()
|
||
data7 = now + datetime.timedelta(days=-7)
|
||
nowstr = now.strftime('%Y-%m-%d')
|
||
last_dtstr = data7.strftime('%Y-%m-%d')
|
||
starttime = last_dtstr+" 00:00:01"
|
||
endtime = nowstr + " 23:59:59"
|
||
return starttime,endtime
|
||
|
||
def get30daysRange():
|
||
"""
|
||
获取近30天时间范围如:2023-08-02 00:00:01,2023-08-02 23:59:59
|
||
"""
|
||
now = datetime.datetime.now()
|
||
last_date = now + datetime.timedelta(days=-30)
|
||
nowstr = now.strftime('%Y-%m-%d')
|
||
last_dtstr = last_date.strftime('%Y-%m-%d')
|
||
starttime = last_dtstr+" 00:00:01"
|
||
endtime = nowstr + " 23:59:59"
|
||
return starttime,endtime
|
||
|
||
def getCurrentMonthRange():
|
||
"""
|
||
获取本月时间范围如:2023-08-01 00:00:01,2023-08-31 23:59:59
|
||
"""
|
||
now = datetime.datetime.now()
|
||
nowday = now.day
|
||
nowday =str(nowday) if len(str(nowday)) >1 else "0"+str(nowday)
|
||
nowyear = now.year
|
||
nowmonth = now.month
|
||
starttime = str(nowyear)+"-"+str(nowmonth)+"-01"+" 00:00:01"
|
||
endtime = str(nowyear)+"-"+str(nowmonth)+"-"+ nowday + " 23:59:59"
|
||
return starttime,endtime
|
||
|
||
def getCurrentMonthRangeDate():
|
||
"""
|
||
获取本月时间范围如:2023-08-01,2023-08-31
|
||
"""
|
||
now = datetime.datetime.now()
|
||
nowday = now.day
|
||
nowday =str(nowday) if len(str(nowday)) >1 else "0"+str(nowday)
|
||
nowyear = now.year
|
||
nowmonth = now.month
|
||
starttime = str(nowyear)+"-"+str(nowmonth)+"-01"
|
||
endtime = str(nowyear)+"-"+str(nowmonth)+"-"+ nowday
|
||
return starttime,endtime
|
||
|
||
def getLastMonthRange():
|
||
"""
|
||
获取上个月时间范围如:2023-08-02 00:00:01,2023-08-02 23:59:59
|
||
"""
|
||
now = datetime.datetime.now()
|
||
this_month_start = datetime.datetime(now.year, now.month, 1)
|
||
last_month_end = this_month_start - datetime.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)
|
||
starttime = last_month_start.strftime('%Y-%m-%d')+" 00:00:01"
|
||
endtime = last_month_end.strftime('%Y-%m-%d') + " 23:59:59"
|
||
return starttime,endtime
|
||
|
||
def getLastMonthRangeDate():
|
||
"""
|
||
获取上个月时间范围如:2023-08-01,2023-08-31
|
||
"""
|
||
now = datetime.datetime.now()
|
||
this_month_start = datetime.datetime(now.year, now.month, 1)
|
||
last_month_end = this_month_start - datetime.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)
|
||
starttime = last_month_start.strftime('%Y-%m-%d')
|
||
endtime = last_month_end.strftime('%Y-%m-%d')
|
||
return starttime,endtime
|
||
|
||
def getLast2MonthRangeDate():
|
||
"""
|
||
获取上上个月时间范围如:2023-08-01,2023-08-31
|
||
"""
|
||
now = datetime.datetime.now()
|
||
this_month_start = datetime.datetime(now.year, now.month, 1)
|
||
last_month_end = this_month_start - datetime.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)
|
||
last_2month_end = last_month_start - datetime.timedelta(days=1) + datetime.timedelta(hours=23, minutes=59,seconds=59)
|
||
last_2month_start = datetime.datetime(last_2month_end.year, last_2month_end.month, 1)
|
||
starttime = last_2month_start.strftime('%Y-%m-%d')
|
||
endtime = last_2month_end.strftime('%Y-%m-%d')
|
||
return starttime,endtime
|
||
|
||
|
||
def getCurrentWeekRangeDate():
|
||
"""
|
||
获取本周的日期范围
|
||
"""
|
||
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 datetime.datetime.strftime(monday, "%Y-%m-%d"), datetime.datetime.strftime(sunday, "%Y-%m-%d")
|
||
|
||
def getLast1WeekRangeDate():
|
||
"""
|
||
获取上周的日期范围
|
||
"""
|
||
today = datetime.datetime.today()
|
||
end_time = today - datetime.timedelta(days=today.isoweekday())
|
||
start_time = end_time - datetime.timedelta(days=6)
|
||
return start_time.strftime("%Y-%m-%d"), end_time.strftime("%Y-%m-%d")
|
||
|
||
def getLast2WeekRangeDate():
|
||
"""
|
||
获取上上周的日期范围
|
||
"""
|
||
std,edt = getLast1WeekRangeDate()
|
||
std = datetime.datetime.strptime(std, '%Y-%m-%d')
|
||
edt = datetime.datetime.strptime(edt, '%Y-%m-%d')
|
||
end_time = std - datetime.timedelta(days=1)
|
||
start_time = end_time - datetime.timedelta(days=6)
|
||
return start_time.strftime("%Y-%m-%d"), end_time.strftime("%Y-%m-%d")
|
||
|
||
def getLast3WeekRangeDate():
|
||
"""
|
||
获取上上上周的日期范围
|
||
"""
|
||
std,edt = getLast1WeekRangeDate()
|
||
std = datetime.datetime.strptime(std, '%Y-%m-%d')
|
||
edt = datetime.datetime.strptime(edt, '%Y-%m-%d')
|
||
end_time = std - datetime.timedelta(days=1)
|
||
start_time = end_time - datetime.timedelta(days=6)
|
||
return start_time.strftime("%Y-%m-%d"), end_time.strftime("%Y-%m-%d") |