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
defget_next_month()
:"""
計算下月起始與結束日期
:return:
"""now = datetime.datetime.now(
) next_month = now.month +
1 year = now.year
if now.month ==12:
next_month =
1if 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"
) start += datetime.timedelta(days=1)
return date_list
9.判定給定日期是否為週末
import datetime
defgetweekday
(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 時間模組小結
python有兩個重要的時間模組,分別是time和datetime 時間元組 time.struct time tm year 2016,tm mon 7,tm mday 21,tm hour 22,tm min 32,tm sec 51,tm wday 3,tm yday 203,tm isdst...
Python執行時間的計算方法小結
首先說一下我遇到的坑,生產上遇到的問題,我調程式設計客棧度python指令碼執行並監控這個程序,python指令碼執行時間遠遠大於python指令碼中自己統計的程式執行時間。監控python指令碼執行的時間是36個小時,而python指令碼中統計自己執行的時間是4個小時左右。問題暴漏之後首先想到的是...
C 計算時間間隔的方法小結
初始化兩個時間變數用於演示例項。datetime dt1 new datetime 2013,10,13,19,15,50 datetime dt2 new datetime 2013,10,13,19,18,50 下面以計算兩個時間相隔總秒數為例。方法一 timespan ts1 dt2.subt...