1.计算天数
import time
import datetime
t1='2020-02-24 16:08:42'
t2='2020-02-24 18:48:53'
d1=datetime.datetime.strptime(t1,'%Y-%m-%d %H:%M:%S')
d2=datetime.datetime.strptime(t2,'%Y-%m-%d %H:%M:%S')
delta=d2-d1
print(delta.days)
2.比较时间大小
import time
t1='2020-02-24 16:08:42'
t2='2020-02-24 18:48:53'
s1=time.strptime(t1,'%Y-%m-%d %H:%M:%S')
s2=time.strptime(t2,'%Y-%m-%d %H:%M:%S')
timeStamp1 = int(time.mktime(s1))
timeStamp2 = int(time.mktime(s2))
print(timeStamp1,timeStamp2)
print(timeStamp2-timeStamp1)
3.判定给定日期是否在今天之内
current_time=time.time()
c=time.localtime(current_time)
str_day=time.strftime("%Y-%m-%d %H:%M:%S", c)
# print(str_day.split(' '))
begin_today=str_day.split(' ')[0]+' 00:00:00'
print(begin_today)
begin_time=time.mktime(time.strptime(begin_today,'%Y-%m-%d %H:%M:%S'))
tar_time=time.mktime(time.strptime(t2,'%Y-%m-%d %H:%M:%S'))
if current_time>tar_time and tar_time>begin_time:
print(True)
else:
print(False)
4.计算本周、上周起始与结束日期
now = datetime.datetime.now()
def get_current_week():
"""
返回当前时间的上一周的开始和结束时间
"""
this_week_start = now - datetime.timedelta(days=now.weekday())
this_week_end = now + datetime.timedelta(days=6 - now.weekday())
return this_week_start.strftime("%Y-%m-%d"), this_week_end.strftime("%Y-%m-%d")
def get_last_week():
"""
获取上周起始与结束日期
:return:
"""
last_week_start = now - datetime.timedelta(days=now.weekday() + 7)
last_week_end = now - datetime.timedelta(days=now.weekday() + 1)
return last_week_start.strftime("%Y-%m-%d"),last_week_end.strftime("%Y-%m-%d")
5.计算本月起始与结束日期
def get_current_month():
"""
计算本月起始与结束日期
:return:
"""
this_month_start = datetime.datetime(now.year, now.month, 1)
this_month_end = datetime.datetime(now.year, now.month + 1, 1) - datetime.timedelta(days=1) + datetime.timedelta(
hours=23, minutes=59, seconds=59)
return this_month_start.strftime("%Y-%m-%d"),this_month_end.strftime("%Y-%m-%d")
6.计算上月起始与结束日期
def get_last_month():
"""
计算上月起始与结束日期
:return:
"""
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)
return last_month_start.strftime("%Y-%m-%d"),last_month_end.strftime("%Y-%m-%d")
**7.计算下月起始与结束日期**
```python
def get_next_month():
"""
计算下月起始与结束日期
:return:
"""
now = datetime.datetime.now()
next_month = now.month + 1
year = now.year
if now.month == 12:
next_month = 1
if now.month + 2 >= 13:
next_month = 1
year += 1
next_month_start = datetime.datetime(year, next_month, 1)
next_month_end = datetime.datetime(year, next_month+1, 1) - datetime.timedelta(
days=1) + datetime.timedelta(
hours=23, minutes=59, seconds=59)
return next_month_start.strftime("%Y-%m-%d"), next_month_end.strftime("%Y-%m-%d")
8.获取两个日期之间的所有日期列表
def getEveryDay(start_date, end_date):
date_list = []
start = datetime.datetime.strptime(start_date, "%Y-%m-%d")
end = datetime.datetime.strptime(end_date, "%Y-%m-%d")
while start <= end:
date_str = start.strftime("%Y-%m-%d")
date_list.append(date_str)
start += datetime.timedelta(days=1)
return date_list
9.判定给定日期是否为周末
import datetime
def getWeekday(date):
date_list=[int(s) for s in date.split('-')]
weekday=datetime.datetime(*date_list).strftime("%w") #参数需要为整型
return weekday
w=getWeekday('2020-11-01')
print(w) #0 周日为0,注意是字符串类型
本文详细介绍如何使用Python进行日期和时间的计算,包括计算天数差异、比较时间大小、判断日期是否在今天内、获取本周及上周日期范围、计算本月及上月日期范围、判断给定日期是否为周末等实用技巧。
155

被折叠的 条评论
为什么被折叠?



