python中時(shí)間模塊的基本使用教程
前言:
在開發(fā)中經(jīng)常會(huì)與時(shí)間打交道,如:獲取事件戳,時(shí)間戳的格式化等,這里簡(jiǎn)要記錄一下python操作時(shí)間的方法。
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:01
2.日期字符串 --> 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
喜歡點(diǎn)個(gè)贊!
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
python連接數(shù)據(jù)庫(kù)后通過占位符添加數(shù)據(jù)
在pymysql中支持對(duì)占位符的處理,開發(fā)者需要在SQL中使用“%”定義占位符,在使用excute()方法執(zhí)行時(shí)對(duì)占位符的數(shù)據(jù)進(jìn)行填充即可,本文給大家介紹python連接數(shù)據(jù)庫(kù)后通過占位符添加數(shù)據(jù)的方法,需要的朋友參考下吧2021-12-12使用Python進(jìn)行數(shù)獨(dú)求解詳解(一)
本文主要介紹了如何構(gòu)建一個(gè)Python腳本來解決數(shù)獨(dú)難題,本文的重點(diǎn)在于介紹用于構(gòu)建數(shù)獨(dú)求解器的回溯算法。感興趣的小伙伴可以學(xué)習(xí)一下2022-02-02python3中函數(shù)參數(shù)的四種簡(jiǎn)單用法
這篇文章主要介紹了python3中函數(shù)參數(shù)的四種簡(jiǎn)單用法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07python 獲取網(wǎng)頁編碼方式實(shí)現(xiàn)代碼
這篇文章主要介紹了python 獲取網(wǎng)頁編碼方式實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03解析Anaconda創(chuàng)建python虛擬環(huán)境的問題
這篇文章主要介紹了Anaconda創(chuàng)建python虛擬環(huán)境,包括虛擬環(huán)境管理、虛擬環(huán)境中python包管理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03