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

python中常見的處理時(shí)間的模塊:
time:處理時(shí)間的模塊,如獲取時(shí)間戳,格式化日期等
datetime:date和time的結(jié)合體,處理日期和時(shí)間
calendar:日歷相關(guān)的模塊,如:處理年歷/月歷
time模塊介紹
說明:time模塊主要講解如下內(nèi)容:
1.時(shí)間戳 --> 時(shí)間元組格式(time.struct_time) --> 日期字符串
2.日期字符串 --> 時(shí)間元組格式(time.struct_time) --> 時(shí)間戳
3.獲取當(dāng)前時(shí)間的分鐘/秒
4.獲取整分鐘/整小時(shí)時(shí)間戳
1.時(shí)間戳 --> 時(shí)間元組格式(time.struct_time) --> 日期字符串
時(shí)間戳 --> 時(shí)間元組格式
time.localtime(timestamp) # 參數(shù)timestamp為秒級(jí)時(shí)間戳
例子:
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)
時(shí)間元組 --> 日期字符串
time.strftime(format, p_tuple=None):format:格式化的日期樣式;p_tuple:時(shí)間元組
例子:
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 時(shí)間戳格式化
:param timestamp: 時(shí)間戳
:return: 格式化后的日期
"""
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))2.日期字符串 --> 時(shí)間元組格式(time.struct_time) --> 時(shí)間戳
日期字符串 --> 時(shí)間元組
time.strptime(string, format) # string:日期字符串,format:該日期字符串對(duì)應(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)時(shí)間元組 --> 時(shí)間戳
time.mktime(p_tuple):p_tuple:時(shí)間元組
例子:
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)換為時(shí)間戳 :param date_str: 日期字符串,如:2019-01-30 11:48:07 :param format: 日期字符串對(duì)應(yīng)的格式化格式,如:%Y-%m-%d %H:%M:%S :return: 時(shí)間戳 """ return int(time.mktime(time.strptime(date_str, format)))
3.獲取當(dāng)前時(shí)間的分鐘/秒
獲取當(dāng)前時(shí)間戳
timestamp = int(time.time())
獲取當(dāng)前時(shí)間的秒
seconds = timestamp % 60
print "seconds:{}".format(seconds)獲取當(dāng)前時(shí)間的分鐘
minute = (timestamp - seconds) % (60 * 60)
print "minute:{}".format(minute / 60)4.獲取整分鐘/整小時(shí)時(shí)間戳
思路:
先除以對(duì)應(yīng)的進(jìn)制值取整,得到舍棄余數(shù)部分的整數(shù),然后再乘以對(duì)應(yīng)的進(jìn)制值
one_minute = 60 # 一分鐘 one_hour = one_minute * 60 # 一小時(shí) whole_minute = int(timestamp / one_minute) * one_minute whole_hour = int(timestamp / one_hour) * one_hour
datetime模塊介紹
datetime模塊中常見的類:
datetime.date:處理日期
datetime.time:處理時(shí)間
datetime.datetime:處理日期和時(shí)間
datetime.timedelta:處理時(shí)間差
說明:datetime模塊主要講解如下內(nèi)容
1.時(shí)間戳 --> datetime時(shí)間格式 --> 日期字符串
2.日期字符串 --> datetime時(shí)間格式 --> 時(shí)間元組格式(time.struct_time) --> 時(shí)間戳
3.時(shí)間差的使用,根據(jù)當(dāng)前時(shí)間獲取前N天的時(shí)間
1.時(shí)間戳 --> datetime時(shí)間格式 --> 日期字符串
時(shí)間戳 --> datetime時(shí)間格式
datetime.datetime.fromtimestamp(timestamp) 參數(shù)timestamp:時(shí)間戳
例子:
import time, datetime datetime_type = datetime.datetime.fromtimestamp(time.time()) print type(datetime_type) # <type 'datetime.datetime'>
datetime時(shí)間格式 --> 日期字符串
datetime.datetime.strftime(format) format:日期字符串對(duì)應(yīng)的格式化格式
例子:
datetime_format = datetime_type.strftime("%Y/%m/%d %H:%M:%S")
print datetime_format # 2019/01/30 16:44:012.日期字符串 --> datetime時(shí)間格式 --> 時(shí)間元組格式(time.struct_time) --> 時(shí)間戳
日期字符串 --> datetime時(shí)間格式
datetime.datetime.strptime(date_str, format) date_str:字符串日期 format:日期字符串對(duì)應(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時(shí)間格式 --> 時(shí)間元組格式(time.struct_time) --> 時(shí)間戳
datetime.datetime.timetuple(): datetime轉(zhuǎn)換為時(shí)間元組
例子:
datetime_type_to_timestamp = int(time.mktime(datetime_type.timetuple())) print datetime_type_to_timestamp
3.時(shí)間差的使用,根據(jù)當(dāng)前時(shí)間獲取前N天的時(shí)間
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:小時(shí)
7.weeks:星期 1weeks = 7days
例子:
day_timedelta = datetime.timedelta(days=1) # 獲取1天的時(shí)間值 forward_datetime = datetime.datetime.today() - day_timedelta # 獲取前一天的datetime值 print forward_datetime
calendar模塊介紹
說明:
這里介紹一下使用month(year, month)方法打印出某年某月下的月歷時(shí)間
例子:
import calendar cal = calendar.month(2019, 1) # 打印出2019年1月的月歷 print cal
關(guān)于如何在python中使用時(shí)間模塊問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
文章題目:如何在python中使用時(shí)間模塊-創(chuàng)新互聯(lián)
URL分享:http://chinadenli.net/article10/ceosdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、外貿(mào)建站、軟件開發(fā)、靜態(tài)網(wǎng)站、動(dòng)態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容