python?time模塊計算時間之間的差距(練習題)
更新時間:2023年05月15日 10:26:01 作者:Hy海洋
這篇文章主要介紹了python?time模塊計算時間之間的差距,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
python time模塊計算時間之間的差距
練習題
1. 當前月1號對應的0點的時間戳
# 定義一個當前月分的一號0點字符串格式的時間
now_time = time.strftime('%Y-%m-01 00:00:00')
# 將格式化時間轉換為結構化時間
jiegou = time.strptime(now_time, '%Y-%m-%d %H:%M:%S')
# 將結構化時間轉換為對應的時間戳
shijiancuo = time.mktime(jiegou)
print('%s對應的時間戳為%s'%(now_time,shijiancuo))2. n1的時間 n2的時間 n2 - n1的時間經(jīng)歷里多少年 月 日 時 分 秒
思想:需要首先將兩個字符串時間轉換為時間戳格式,然后相減,再轉換為結構化時間,然后減去時間戳最開始時間(倫敦時間:1970/01/01 00:00:00)
import time
n1 = '2019-07-18 20:07:56'
n2 = '2019-07-19 22:03:12'
# 格式化時間轉換為結構化時間
struct_time1, struct_time2 = time.strptime(n1, '%Y-%m-%d %H:%M:%S'), time.strptime(n2, '%Y-%m-%d %H:%M:%S')
# 結構化時間轉換為時間戳格式
struct_time1, struct_time2 = time.mktime(struct_time1), time.mktime(struct_time2)
# 差的時間戳
diff_time = struct_time2 - struct_time1
# 將計算出來的時間戳轉換為結構化時間
struct_time = time.gmtime(diff_time)
# 減去時間戳最開始的時間 并格式化輸出
print('過去了{0}年{1}月{2}日{3}小時{4}分鐘{5}秒'.format(
struct_time.tm_year-1970,
struct_time.tm_mon-1,
struct_time.tm_mday-1,
struct_time.tm_hour,
struct_time.tm_min,
struct_time.tm_sec
))補充:python-time模塊計算時間差
import time
# t = time.time()
# print(t)
# z = time.strftime('%Y-%m-%d %H:%M:%S')
# print(z)
#
# a = time.localtime(time.time())
# print(a)
nowtime = time.time()
longtime = time.strptime('2018-10-17 6:0:0','%Y-%m-%d %H:%M:%S')
print(longtime)
d = time.mktime(longtime)
print(d)
new = nowtime - d
print(new)
s = time.localtime(new)
ss = time.strftime('%H:%M:%S',time.localtime(new))
print(ss)到此這篇關于python time模塊計算時間之間的差距的文章就介紹到這了,更多相關python time模塊計算時間內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Flask使用Pyecharts在單個頁面展示多個圖表的方法
這篇文章主要介紹了Flask使用Pyecharts在單個頁面展示多個圖表的方法,在Flask頁面展示echarts,主要有兩種方法,文中給大家介紹的非常詳細,需要的朋友可以參考下2019-08-08
Python通過VGG16模型實現(xiàn)圖像風格轉換操作詳解
這篇文章主要介紹了Python通過VGG16模型實現(xiàn)圖像風格轉換操作,結合實例形式詳細分析了Python使用VGG16模型實現(xiàn)圖像風格轉換的具體原理、操作步驟與實現(xiàn)方法,需要的朋友可以參考下2020-01-01

