python3常用時間處理工具整理 實用工作備忘

2021-10-10 02:44:51 字數 3663 閱讀 4672

整理工作中常用的時間處理方法

# coding:utf-8

import time

import datetime

defgap_day

(time_str, date=

none

,format

="%y-%m-%d"):

""" :判斷自然日差幾天

:param time_str: 2019-02-19 21:49:20

:return:

"""ifnot date:

date = time.strftime(

format

) now_day_stamps = time.mktime(time.strptime(date,

format))

if" "

informat

:format

=format

.split(

" ")[0

] input_stamps = time.mktime(time.strptime(time_str.split(

" ")[0

],format))

return

int(

abs(

(now_day_stamps - input_stamps))//

86400

)def

gap_stamps_day

(time_str, now_day_stamps=

none):

""" :判斷絕對時間差幾天

:param time_str: 2019-02-19 21:49:20

:return:

"""ifnot now_day_stamps:

now_day_stamps = time.time(

) input_stamps = time.mktime(time.strptime(time_str,

"%y-%m-%d %h:%m:%s"))

return

int(

abs(

(now_day_stamps - input_stamps))//

86400

)def

gap_stamps_hour

(time_str)

:"""

:判斷絕對上差幾個小時

:param time_str: 2019-02-19 21:49:20

:return:

"""now =

int(time.time())

input_stamps = time.mktime(time.strptime(time_str,

"%y-%m-%d %h:%m:%s"))

return

int(

abs(

(now - input_stamps))//

3600

)def

gap_stamps_minute

(time_str)

:"""

:判斷絕對上差幾個分鐘

:param time_str: 2019-02-19 21:49:20

:return:

"""now =

int(time.time())

input_stamps = time.mktime(time.strptime(time_str,

"%y-%m-%d %h:%m:%s"))

return

int(

abs(

(now - input_stamps))//

60)defget_time_stamp

(timestr,

format

="%y-%m-%d %h:%m:%s"):

""" 獲取時間戳

:param timestr: 2019-05-12 09:00:00

:param format: "%y-%m-%d %h:%m:%s"

:return:

"""timearray = time.strptime(timestr,

format

) timestamp = time.mktime(timearray)

return

int(timestamp)

defgap_stamps_sec

(time_str)

:"""

:判斷絕對上差幾秒

:param time_str: 2019-02-19 21:49:20

:return:

"""now =

int(time.time())

input_stamps = time.mktime(time.strptime(time_str,

"%y-%m-%d %h:%m:%s"))

return

int(

abs(

(now - input_stamps)))

defget_day_jia

(date, n,

format

='%y-%m-%d'):

""" 時間加n天

:param date:

:param n:

:return:

"""jia_date = datetime.datetime.strptime(date,

format

) jia_date = jia_date + datetime.timedelta(days=n)

return jia_date.strftime(

format

)def

get_sec_jia

(seconds,

format

="%y-%m-%d %h:%m:%s"):

""" 當前時間加多少秒

:param seconds:

:param format:

:return: 2019-12-30 19:25:30

"""return

(datetime.datetime.now(

)+ datetime.timedelta(seconds=seconds)

).strftime(

format

)def

get_ts_to_time

(ts)

:"""時間戳轉字串"""

return time.strftime(

"%y-%m-%d %h:%m:%s"

, time.localtime(ts)

)def

day_long

(time1, time2)

:"""

計算時間差

:return: 相差的天數

"""ts1 = get_time_stamp(time1)

ts2 = get_time_stamp(time2)

date1 = datetime.datetime.fromtimestamp(ts1)

date2 = datetime.datetime.fromtimestamp(ts2)

return

abs(

int(

(date2 - date1)

.days)

)

參考鏈結

常用時間處理05

pandas中,to datetime 函式是用來進行獲取時間和進行時間資料操作的函式。dates pd.to datetime pd.series 1989 8 18 13 14 55 1995 2 16 format y m d h m s 根據面臨的資料格式不同,還有以下幾種常用的方法 pri...

SQL 常用時間處理

本文適用於sql server資料庫的t sql語法,我只是個搬運工,進行簡單歸類,微軟官網國內可以直接訪問,我就不截圖和專門整理了,想看詳情,直接點選函式 日期轉換 convert varchar 20 getdate 120 其中20可以替換成自己想要的長度,進行一定程度的擷取,其中的120可以...

python3時間 Python3 時間處理

usr bin python3 import time 引入time模組 python3 日期和時間 當前時間戳 ticks time.time print 當前時間戳為 ticks 當前時間戳為 1522024006.313911 完全版本地時間 localtime time.localtime ...