python獲取指定時間差的時間實例詳解
python獲取指定時間差的時間實例詳解
在分析數(shù)據(jù)的時間經(jīng)常需要截取一定范圍時間的數(shù)據(jù),比如三天之內(nèi),兩小時前等等時間要求的數(shù)據(jù),因此將該部分經(jīng)常需要用到的功能模塊化,方便以后以后用到的時候復(fù)用。在此,也分享給大家。
import time
import sys
reload(sys)
def get_day_of_day(UTC=False, days=0, hours=0, miutes=0, seconds=0):
'''''''
if days>=0,date is larger than today
if days<0,date is less than today
date format = "YYYY-MM-DD"
'''
now = time.time()
timeNew = now + days*24*60*60 + hours*60*60 + miutes*60 + seconds
if UTC :
timeNew = timeNew + time.timezone
t = time.localtime(timeNew)
return time.strftime('%Y-%m-%d %H:%M:%S', t)
#使用UTC時間 兩小時前
t = get_day_of_day(True,0,-2)
print t
#當(dāng)?shù)貢r間 三天前
t = get_day_of_day(False,-3)
print t
#當(dāng)?shù)貢r間 三天后
t = get_day_of_day(False,3)
print t
運行后所得結(jié)果:
2016-04-30 20:25:56 2016-05-06 20:25:56
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
詳解duck typing鴨子類型程序設(shè)計與Python的實現(xiàn)示例
這篇文章主要介紹了詳解duck typing鴨子類型程序設(shè)計與Python的實現(xiàn)示例,鴨子類型特指解釋型語言中的一種編程風(fēng)格,需要的朋友可以參考下2016-06-06
Python操作數(shù)據(jù)庫之?dāng)?shù)據(jù)庫編程接口
這篇文章主要介紹了Python操作數(shù)據(jù)庫之?dāng)?shù)據(jù)庫編程接口,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-06-06
Python操作使用MySQL數(shù)據(jù)庫的實例代碼
本篇文章主要介紹了Python 操作 MySQL的實例代碼,詳細介紹了Python如何連接數(shù)據(jù)庫和對數(shù)據(jù)的增刪查改,有興趣的可以了解一下2017-05-05
Python報錯TypeError: unhashable type: ‘numpy.nd
在Python編程中,尤其是在處理數(shù)據(jù)時,我們經(jīng)常使用numpy數(shù)組,然而,當(dāng)我們嘗試將numpy數(shù)組用作字典的鍵或集合的元素時,就會遇到TypeError: unhashable type: 'numpy.ndarray',本文將探討這個錯誤的原因,并給出幾種可能的解決方案,需要的朋友可以參考下2024-09-09

