如何在python中使用時間模塊-創(chuàng)新互聯(lián)

如何在python中使用時間模塊?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

成都創(chuàng)新互聯(lián)長期為上千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為新城企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、成都做網(wǎng)站新城網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

python中常見的處理時間的模塊:

  • time:處理時間的模塊,如獲取時間戳,格式化日期等

  • datetime:date和time的結(jié)合體,處理日期和時間

  • calendar:日歷相關(guān)的模塊,如:處理年歷/月歷

time模塊介紹

說明:time模塊主要講解如下內(nèi)容:

1.時間戳  --> 時間元組格式(time.struct_time)   -->  日期字符串

2.日期字符串 -->  時間元組格式(time.struct_time)  --> 時間戳

3.獲取當(dāng)前時間的分鐘/秒

4.獲取整分鐘/整小時時間戳

1.時間戳  --> 時間元組格式(time.struct_time)   -->  日期字符串

時間戳  --> 時間元組格式

time.localtime(timestamp)  # 參數(shù)timestamp為秒級時間戳

例子:

import time

time_tuple = time.localtime(time.time())
print time_tuple # time.struct_time(tm_year=2019, tm_mon=1, tm_mday=30, tm_hour=11, tm_min=29, tm_sec=33, tm_wday=2, tm_yday=30, tm_isdst=0)

時間元組 --> 日期字符串

time.strftime(format, p_tuple=None):format:格式化的日期樣式;p_tuple:時間元組

例子:

time_format = time.strftime("%Y-%m-%d %H:%M:%S", time_tuple)
print time_format # 2019-01-30 11:48:07

封裝成方法:

def timestamp_format(timestamp):
 """
 :brief 時間戳格式化
 :param timestamp: 時間戳
 :return: 格式化后的日期
 """
 return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))

2.日期字符串 --> 時間元組格式(time.struct_time) --> 時間戳

日期字符串 -->  時間元組

time.strptime(string, format) # string:日期字符串,format:該日期字符串對應(yīng)的格式化格式

例子:

import time

time_str_to_tuple = time.strptime("2019-01-30 11:48:07", "%Y-%m-%d %H:%M:%S")
print time_str_to_tuple # time.struct_time(tm_year=2019, tm_mon=1, tm_mday=30, tm_hour=11, tm_min=48, tm_sec=7, tm_wday=2, tm_yday=30, tm_isdst=-1)

時間元組  --> 時間戳

time.mktime(p_tuple):p_tuple:時間元組

例子:

time_tuple_to_timestamp = int(time.mktime(time_str_to_tuple))
print time_tuple_to_timestamp # 結(jié)果:1548820087

封裝成方法

def time_str_to_timestamp(date_str, format):
 """
 :brief 將字符串日期轉(zhuǎn)換為時間戳
 :param date_str: 日期字符串,如:2019-01-30 11:48:07
 :param format: 日期字符串對應(yīng)的格式化格式,如:%Y-%m-%d %H:%M:%S
 :return: 時間戳
 """
 return int(time.mktime(time.strptime(date_str, format)))

3.獲取當(dāng)前時間的分鐘/秒

獲取當(dāng)前時間戳

timestamp = int(time.time())

獲取當(dāng)前時間的秒

seconds = timestamp % 60
print "seconds:{}".format(seconds)

獲取當(dāng)前時間的分鐘

minute = (timestamp - seconds) % (60 * 60)
print "minute:{}".format(minute / 60)

4.獲取整分鐘/整小時時間戳

思路:

先除以對應(yīng)的進(jìn)制值取整,得到舍棄余數(shù)部分的整數(shù),然后再乘以對應(yīng)的進(jìn)制值

one_minute = 60 # 一分鐘
one_hour = one_minute * 60 # 一小時

whole_minute = int(timestamp / one_minute) * one_minute
whole_hour = int(timestamp / one_hour) * one_hour

datetime模塊介紹

datetime模塊中常見的類:

  • datetime.date:處理日期

  • datetime.time:處理時間

  • datetime.datetime:處理日期和時間

  • datetime.timedelta:處理時間差

說明:datetime模塊主要講解如下內(nèi)容

1.時間戳  --> datetime時間格式   -->  日期字符串

2.日期字符串 -->  datetime時間格式  --> 時間元組格式(time.struct_time) --> 時間戳

3.時間差的使用,根據(jù)當(dāng)前時間獲取前N天的時間

1.時間戳  --> datetime時間格式   -->  日期字符串

時間戳  --> datetime時間格式

datetime.datetime.fromtimestamp(timestamp) 參數(shù)timestamp:時間戳

例子:

import time, datetime

datetime_type = datetime.datetime.fromtimestamp(time.time())
print type(datetime_type) # <type 'datetime.datetime'>

datetime時間格式   -->  日期字符串

datetime.datetime.strftime(format) format:日期字符串對應(yīng)的格式化格式

例子:

datetime_format = datetime_type.strftime("%Y/%m/%d %H:%M:%S")
print datetime_format # 2019/01/30 16:44:01

2.日期字符串 -->  datetime時間格式  --> 時間元組格式(time.struct_time) --> 時間戳

日期字符串 -->  datetime時間格式

datetime.datetime.strptime(date_str, format) date_str:字符串日期    format:日期字符串對應(yīng)的格式化格式

例子:

datetime_type = datetime.datetime.strptime('2019/01/30 16:44:01', '%Y/%m/%d %H:%M:%S')
print type(datetime_type) # <type 'datetime.datetime'>
# print datetime_type.timestamp()
print time.mktime(datetime_type.timetuple())

datetime時間格式  --> 時間元組格式(time.struct_time) --> 時間戳

datetime.datetime.timetuple(): datetime轉(zhuǎn)換為時間元組

例子:

datetime_type_to_timestamp = int(time.mktime(datetime_type.timetuple()))
print datetime_type_to_timestamp

3.時間差的使用,根據(jù)當(dāng)前時間獲取前N天的時間

datetime.timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks)

參數(shù)說明:

1.days:天

2.seconds:秒

3.microseconds:毫秒      1秒 = 10^3 毫秒

4.milliseconds:微秒      1秒 = 10^6 微秒

5.minutes,分鐘

6.hours:小時

7.weeks:星期    1weeks = 7days

例子:

day_timedelta = datetime.timedelta(days=1) # 獲取1天的時間值
forward_datetime = datetime.datetime.today() - day_timedelta # 獲取前一天的datetime值
print forward_datetime

calendar模塊介紹

說明:

這里介紹一下使用month(year, month)方法打印出某年某月下的月歷時間

例子:

import calendar
cal = calendar.month(2019, 1) # 打印出2019年1月的月歷
print cal

關(guān)于如何在python中使用時間模塊問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

文章題目:如何在python中使用時間模塊-創(chuàng)新互聯(lián)
URL分享:http://www.muchs.cn/article10/ceosdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站外貿(mào)建站、軟件開發(fā)、靜態(tài)網(wǎng)站動態(tài)網(wǎng)站、網(wǎng)站設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)